BitString type¶
- class pyasn1.type.univ.BitString(value=NoValue(), tagSet=TagSet(), subtypeSpec=ConstraintsIntersection(), namedValues=NamedValues(), binValue=NoValue(), hexValue=NoValue())¶
Create BitString schema or value object.
BitString class is based on
SimpleAsn1Type
, its objects are immutable and duck-type both Pythontuple
(as a tuple of bits) andint
objects.- Keyword Arguments
value (
int
,str
or BitString object) – Pythonint
orstr
literal representing binary or hexadecimal number or sequence of integer bits or BitString object. If value is not given, schema object will be created.tagSet (
TagSet
) – Object representing non-default ASN.1 tag(s)subtypeSpec (
ConstraintsIntersection
) – Object representing non-default ASN.1 subtype constraint(s). Constraints verification for BitString type occurs automatically on object instantiation.namedValues (
NamedValues
) – Object representing non-default symbolic aliases for numbersbinValue (
str
) – Binary string initializer to use instead of the value. Example: ‘10110011’.hexValue (
str
) – Hexadecimal string initializer to use instead of the value. Example: ‘DEADBEEF’.
- Raises
ValueConstraintError, PyAsn1Error – On constraint violation or bad initializer.
Examples
class Rights(BitString): ''' ASN.1 specification: Rights ::= BIT STRING { user-read(0), user-write(1), group-read(2), group-write(3), other-read(4), other-write(5) } group1 Rights ::= { group-read, group-write } group2 Rights ::= '0011'B group3 Rights ::= '3'H ''' namedValues = NamedValues( ('user-read', 0), ('user-write', 1), ('group-read', 2), ('group-write', 3), ('other-read', 4), ('other-write', 5) ) group1 = Rights(('group-read', 'group-write')) group2 = Rights('0011') group3 = Rights(0x3)
Note
The BitString type models an arbitrary sequence of bits.
- clone(value=NoValue(), tagSet=TagSet(), subtypeSpec=ConstraintsIntersection(), namedValues=NamedValues(), binValue=NoValue(), hexValue=NoValue())¶
Create a modified version of BitString schema or value object.
The clone() method accepts the same set arguments as BitString class takes on instantiation except that all arguments of the clone() method are optional.
Whatever arguments are supplied, they are used to create a copy of self taking precedence over the ones used to instantiate self.
Note
Due to the immutable nature of the BitString object, if no arguments are supplied, no new BitString object will be created and self will be returned instead.
- subtype(value=NoValue(), implicitTag=Tag(), explicitTag=Tag(), subtypeSpec=ConstraintsIntersection(), namedValues=NamedValues(), binValue=NoValue(), hexValue=NoValue())¶
Create a specialization of BitString schema or value object.
The subtype relationship between ASN.1 types has no correlation with subtype relationship between Python types. ASN.1 type is mainly identified by its tag(s) (
TagSet
) and value range constraints (ConstraintsIntersection
). These ASN.1 type properties are implemented as BitString attributes.The subtype() method accepts the same set arguments as BitString class takes on instantiation except that all parameters of the subtype() method are optional.
With the exception of the arguments described below, the rest of supplied arguments they are used to create a copy of self taking precedence over the ones used to instantiate self.
The following arguments to subtype() create a ASN.1 subtype out of BitString type:
- Other Parameters
implicitTag (
Tag
) – Implicitly apply given ASN.1 tag object to self’sTagSet
, then use the result as new object’s ASN.1 tag(s).explicitTag (
Tag
) – Explicitly apply given ASN.1 tag object to self’sTagSet
, then use the result as new object’s ASN.1 tag(s).subtypeSpec (
ConstraintsIntersection
) – Add ASN.1 constraints object to one of the self’s, then use the result as new object’s ASN.1 constraints.
- Returns
new instance of BitString schema or value object
Note
Due to the immutable nature of the BitString object, if no arguments are supplied, no new BitString object will be created and self will be returned instead.
- tagSet = <TagSet object, tags 0:0:3>¶
Set (on class, not on instance) or return a
TagSet
object representing ASN.1 tag(s) associated with BitString type.
- subtypeSpec = <ConstraintsIntersection object>¶
Set (on class, not on instance) or return a
ConstraintsIntersection
object imposing constraints on BitString type initialization values.
- asNumbers()¶
Get BitString value as a sequence of 8-bit integers.
If BitString object length is not a multiple of 8, result will be left-padded with zeros.
- asOctets()¶
Get BitString value as a sequence of octets.
If BitString object length is not a multiple of 8, result will be left-padded with zeros.
- asInteger()¶
Get BitString value as a single integer value.
- asBinary()¶
Get BitString value as a text string of bits.
- classmethod fromHexString(value, internalFormat=False, prepend=None)¶
Create a BitString object initialized from the hex string.
- Parameters
value (
str
) – Text string like ‘DEADBEEF’
- classmethod fromBinaryString(value, internalFormat=False, prepend=None)¶
Create a BitString object initialized from a string of ‘0’ and ‘1’.
- Parameters
value (
str
) – Text string like ‘1010111’
- classmethod fromOctetString(value, internalFormat=False, prepend=None, padding=0)¶
Create a BitString object initialized from a string.
- property isValue¶
Indicate that BitString object represents ASN.1 value.
If isValue is
False
then this object represents just ASN.1 schema.If isValue is
True
then, in addition to its ASN.1 schema features, this object can also be used like a Python built-in object (e.g.int
,str
,dict
etc.).- Returns
bool
–False
if object represents just ASN.1 schema.True
if object represents ASN.1 schema and can be used as a normal value.
Note
There is an important distinction between PyASN1 schema and value objects. The PyASN1 schema objects can only participate in ASN.1 schema-related operations (e.g. defining or testing the structure of the data). Most obvious uses of ASN.1 schema is to guide serialisation codecs whilst encoding/decoding serialised ASN.1 contents.
The PyASN1 value objects can additionally participate in many operations involving regular Python objects (e.g. arithmetic, comprehension etc).
- isSameTypeWith(other, matchTags=True, matchConstraints=True)¶
Examine BitString type for equality with other ASN.1 type.
ASN.1 tags (
tag
) and constraints (constraint
) are examined when carrying out ASN.1 types comparison.Python class inheritance relationship is NOT considered.
- isSuperTypeOf(other, matchTags=True, matchConstraints=True)¶
Examine BitString type for subtype relationship with other ASN.1 type.
ASN.1 tags (
tag
) and constraints (constraint
) are examined when carrying out ASN.1 types comparison.Python class inheritance relationship is NOT considered.
- property effectiveTagSet¶
For BitString type is equivalent to tagSet