Reputation: 185
I have some structure which I want to read from binary file. I try to use for python's struct. Here a string for reading:
.... = struct.unpack('I36s36s72sQQIIH4s36s4s20s', datab.read(238))
it works fine. I see it by printing values to screen.
Now I want to change "4s" to "I" as it should be. When I change string to:
.... = struct.unpack('I36s36s72sQQIIHI36s4s20s', datab.read(238))
(change first "4s" to "I"), I getting error:
struct.error: unpack requires a bytes object of length 240
As documentation said "I" size is 4 bytes, why my change causes error?
Upvotes: 4
Views: 217
Reputation: 500943
This has to do with alignment. I
gets aligned on a four-byte boundary, requiring two bytes of padding before it. On the other hand, 4s
has no such requirement.
See 7.3.2.1. Byte Order, Size, and Alignment for information on how to turn this off.
Upvotes: 5