API

Packers

Adapters

Numbers

class construct3.numbers.MaskedInteger(underlying, **fields)[source]
>>> m = MaskedInteger(uint16l,
...     bottom4 = (0, 4), 
...     upper12 = (4, 12),
... )
>>> print m.unpack("\x17\x02")
Container:
  bottom4 = 7
  upper12 = 33
>>> print repr(m.pack(Container(upper12 = 33, bottom4 = 7)))
'\x17\x02'

Macros

construct3.macros.AlignedStruct(*members)[source]

Algorithm taken from http://en.wikipedia.org/wiki/Data_structure_alignment#Computing_padding

Example:

s = AlignedStruct(
    "a" / word8,               # 0
    # padding (1)              # 1
    "b" / word16,              # 2-3
    "c" / word16,              # 4-5
    # padding (2)              # 6-7
    "d" / word32,              # 8-11
    "e" / word8,               # 12
    "f" / BitStruct(           # 13
        "x" / nibble,
        "y" / nibble,
    )
    # padding (2)              # 14-15
    # total size = 16
)

Compiler