The ConstBitStream class¶
-
class
bitstring.ConstBitStream([auto, length, offset, **kwargs])¶ The
ConstBitArrayclass is the base class forConstBitStreamand so all of its methods are also available forConstBitStreamobjects. The initialiser is also the same as forConstBitArrayand so won’t be repeated here.A
ConstBitStreamis aConstBitArraywith added methods and properties that allow it to be parsed as a stream of bits.-
bytealign()¶ Aligns to the start of the next byte (so that
posis a multiple of 8) and returns the number of bits skipped.If the current position is already byte aligned then it is unchanged.
>>> s = ConstBitStream('0xabcdef') >>> s.pos += 3 >>> s.bytealign() 5 >>> s.pos 8
-
peek(fmt)¶ Reads from the current bit position
posin the bitstring according to the fmt string or integer and returns the result.The bit position is unchanged.
For information on the format string see the entry for the
readmethod.>>> s = ConstBitStream('0x123456') >>> s.peek(16) ConstBitStream('0x1234') >>> s.peek('hex:8') '0x12'
-
peeklist(fmt, **kwargs)¶ Reads from current bit position
posin the bitstring according to the fmt string or iterable and returns a list of results.A dictionary or keyword arguments can also be provided. These will replace length identifiers in the format string. The position is not advanced to after the read items.
-
read(fmt)¶ Reads from current bit position
posin the bitstring according the the format string and returns a single result. If not enough bits are available then all bits to the end of the bitstring will be used.fmt is either a token string that describes how to interpret the next bits in the bitstring or an integer. If it’s an integer then that number of bits will be read, and returned as a new bitstring. Otherwise the tokens are:
int:nnbits as a signed integer.uint:nnbits as an unsigned integer.float:nnbits as a floating point number.intbe:nnbits as a big-endian signed integer.uintbe:nnbits as a big-endian unsigned integer.floatbe:nnbits as a big-endian float.intle:nnbits as a little-endian signed int.uintle:nnbits as a little-endian unsigned int.floatle:nnbits as a little-endian float.intne:nnbits as a native-endian signed int.uintne:nnbits as a native-endian unsigned int.floatne:nnbits as a native-endian float.hex:nnbits as a hexadecimal string.oct:nnbits as an octal string.bin:nnbits as a binary string.uenext bits as an unsigned exp-Golomb. senext bits as a signed exp-Golomb. uienext bits as an interleaved unsigned exp-Golomb. sienext bits as an interleaved signed exp-Golomb. bits:nnbits as a new bitstring.bytes:nnbytes asbytesobject.boolnext bit as a boolean (True or False). For example:
>>> s = ConstBitStream('0x23ef55302') >>> s.read('hex:12') '0x23e' >>> s.read('bin:4') '0b1111' >>> s.read('uint:5') 10 >>> s.read('bits:4') ConstBitStream('0xa')
The
readmethod is useful for reading exponential-Golomb codes.>>> s = ConstBitStream('se=-9, ue=4') >>> s.read('se') -9 >>> s.read('ue') 4
-
readlist(fmt, **kwargs)¶ Reads from current bit position
posin the bitstring according to the fmt string or iterable and returns a list of results. If not enough bits are available then all bits to the end of the bitstring will be used.A dictionary or keyword arguments can also be provided. These will replace length identifiers in the format string. The position is advanced to after the read items.
See the entry for
readfor information on the format strings.For multiple items you can separate using commas or given multiple parameters:
>>> s = ConstBitStream('0x43fe01ff21') >>> s.readlist('hex:8, uint:6') ['0x43', 63] >>> s.readlist(['bin:3', 'intle:16']) ['0b100', -509] >>> s.pos = 0 >>> s.readlist('hex:b, uint:d', b=8, d=6) ['0x43', 63]
-
bytepos¶ Property for setting and getting the current byte position in the bitstring.
When used as a getter will raise a
ByteAlignErrorif the current position in not byte aligned.
-
pos¶
-