I have a binary file and I've read the first 50 bytes of it.
data = ftw.read(50)
print(f" Read {len(data)} bytes: {data}")
temp_tuple = struct.unpack("HHHL", data[0:10])
When I read the documentation (here: https://docs.python.org/3/library/struct.html#struct.unpack), it would seem that 'H' would specify 2 bytes and 'L' matches to 4 bytes. Therefore, 3 'H' translates into 6 bytes in total and 1 'L' adds another 4 bytes, for a total of 10. So, I don't understand why I'm getting the below error message:
temp_tuple = struct.unpack("HHHL", data[0:10])
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
struct.error: unpack requires a buffer of 12 bytes
The last value 'L' is supposed to be an integer.
Why do I need 12 bytes? What did I not understand in the documentation?
I was expecting for this conversion to work and then I'd get a tuple with 4 variables.
See the note at the top of the
structdocumentation:So specify byte ordering and no padding is used:
Here you can see the two
00padding bytes added so the 4-byte integer is aligned on a modulo 4-byte offset (offset 8) instead of offset 6 when unpadded: