hio.help.packing

hio.help.packing Module

Packs and unpacks bit fields using format string whereas struct in std lib only packs and unpacks byte fields.

Module Contents

hio.help.packing.packify(fmt='8', fields=[0], size=None, reverse=False)

Packs fields sequence of bit fields into bytearray of size bytes using fmt string. Each white space separated field of fmt is the length of the associated bit field If not provided size is the least integer number of bytes that hold the fmt. If reverse is true reverse the order of the bytes in the byte array before returning. This is useful for converting between bigendian and littleendian.

Assumes unsigned fields values. Assumes network big endian so first fields element is high order bits. Each field in format string is number of bits for the associated bit field. Fields with length of 1 are treated as having boolean truthy field values; nonzero is True and packs as a 1. For 2+ length bit fields the field element is truncated to the number of low order bits in the bit field. If sum of number of bits in fmt is less than size bytes then the last byte in the bytearray is right zero padded. If sum of number of bits in fmt is greater than size bytes, returns exception. To pad just use 0 value in source field. Example: packify("1 3 2 2", (True, 4, 0, 3)) returns bytearray([0xc3]).

hio.help.packing.packifyInto(b, fmt='8', fields=[0], size=None, offset=0, reverse=False)

Packs fields sequence of bit fields using fmt string into bytearray b starting at offset and packing into size bytes Each white space separated field of fmt is the length of the associated bit field If not provided size is the least integer number of bytes that hold the fmt. Extends the length of b to accommodate size after offset if not enough. Returns actual size of portion packed into. The default assumes big endian. If reverse is True then reverses the byte order before extending. Useful for little endian.

Assumes unsigned fields values. Assumes network big endian so first fields element is high order bits. Each field in format string is number of bits for the associated bit field. Fields with length of 1 are treated as having boolean truthy field values; nonzero is True and packs as a 1. For 2+ length bit fields the field element is truncated to the number of low order bits in the bit field. If sum of number of bits in fmt is less than size bytes then the last byte in the bytearray is right zero padded. If sum of number of bits in fmt is greater than size bytes, returns exception. To pad just use 0 value in source field. Example: packify("1 3 2 2", (True, 4, 0, 3)) returns bytearray([0xc3]).

hio.help.packing.unpackify(fmt='1 1 1 1 1 1 1 1', b=bytearray([0]), boolean=False, size=None, reverse=False)

Returns tuple of unsigned int bit field values that are unpacked from the bytearray b according to fmt string. b maybe an integer iterator If not provided size is the least integer number of bytes that hold the fmt. The default assumes big endian. If reverse is True then reverse the byte order of b before unpackifing. This is useful for little endian.

Each white space separated field of fmt is the length of the associated bit field. returns unsigned fields values.

Assumes network big endian so first fmt is high order bits. Format string is number of bits per bit field If boolean parameter is True then return boolean values for bit fields of length 1.

If sum of number of bits in fmt is less than 8 * size then remaining bits are returned as additional field in result.

if sum of number of bits in fmt greater 8 * len(b) returns exception

Examples: unpackify(u"1 3 2 2", bytearray([0xc3]), False) returns (1, 4, 0, 3) and unpackify(u"1 3 2 2", 0xc3, True) returns (True, 4, 0, 3).