Core API: Bytes and bits

construct.Bytes(length)

Field consisting of a specified number of bytes. Builds from a bytes, or an integer (although deprecated and BytesInteger should be used instead).

See also

Analog BytesInteger() that parses and builds from integers, as opposed to bytes.

Parameters:length – an integer or a context lambda that returns such an integer

Example:

>>> d = Bytes(4)
>>> d.parse(b'beef')
b'beef'
>>> d.build(b'beef')
b'beef'
>>> d.build(0)
b'\x00\x00\x00\x00'
>>> d.sizeof()
4
construct.GreedyBytes()

Field that parses the stream to the end and builds into the stream as is.

See also

Analog GreedyString() that parses and builds from strings using an encoding.

Example:

>>> GreedyBytes.parse(b"asislight")
b'asislight'
>>> GreedyBytes.build(b"asislight")
b'asislight'
construct.Bitwise(subcon)

Converts the stream from bytes to bits, and passes the bitstream to underlying subcon.

See also

Analog Bytewise() that transforms subset of bits back to bytes.

Warning

Do not use pointers inside this or other restreamed contexts.

Parameters:subcon – any field that works with bits like BitStruct or Bit Nibble Octet BitsInteger

Example:

>>> d = Bitwise(Octet)
>>> d.parse(b"\xff")
255
>>> d.build(1)
b'\x01'
>>> d.sizeof()
1
construct.Bytewise(subcon)

Converts the stream from bits back to bytes. Must be used within Bitwise.

Parameters:subcon – any field that works with bytes

Example:

>>> d = Bitwise(Bytewise(Byte))
>>> d.parse(b"\xff")
255
>>> d.build(255)
b'\xff'
>>> d.sizeof()
1