I’m trying to parse out some data packed into this binary file and Python’s struct module is causing me all sorts of problems. It won’t seem to give me the correct float variable when it’s trying to do more than one type at a time:
import struct
# a fragment of the binary file
a = '\x39\x00\xFF\x00\x00\x0A\x00\x1F\x05\xDC\x42\x31\x30\x00\xFF\x00\x00\x0A\x00\xB5\x01\xE6\x42'
struct.unpack_from('1sxHxbxf', a)
# returns ('9', 255, 10, 2.8355782166755716e-09), but
struct.unpack_from('f',a[7:])
# gives the expected (110.01000213623047,)
The unpacking expects the float to be aligned on an 8-byte boundary and skips over 1 padding byte to get there. You can confirm this by skipping 1 byte yourself:
To disable alignment, add
=,<,>, or!to the front of the format string.