I've got a bytestring "\x56\x20", which is two sets of data, a (12 bits) and b (4 bits).
The unpacked data is expected to be:
a = 86 b = 2
Where:
a = int("056", 16)
b = int("2", 16)
I know I can use binascii to convert the bytestring to a hex string and then work slice magic on it, but that seems messy.
I looked at struct but couldn't figure out a method to split out 12 bits/4 bits.
>>> import binascii
>>> two_octets = "\x56\x20"
>>> hex_str = binascii.hexlify(two_octets)
>>> temp_a, temp_b = hex_str[:2], hex_str[2:]
>>> a_part, b_part = reversed([c for c in temp_b])
>>> int(a_part + temp_a, 16)
86
>>> int(b_part, 16)
2
>>>
Is there a cleaner way?
\x56\x20is to be split in a 12 and a 4 bit section, you get 1378 and 0, not 86 and 2.. Unless this is little-endian, and thus should be interpreted as\x20\x56really.