Reputation: 22569
I'm trying to use Struct
in python to pack some data, but experiencing some weird behavior:
My format is:
struct.Struct('B B I 15s I')
The output is (Pipes added for clarity):
ff|01|000022000000|4650766e65564369797a4531416f41|0001000000
The first two entries are perfect, and so is the string in the middle. But the integer values I am passing are 34 and 1, respectively, and would expect 00000022, 00000001 instead of the weird 6 byte data that I am getting...
Upvotes: 2
Views: 226
Reputation: 500913
There are two issues at play: endianness and padding. For example, the 00002200000000
is two bytes of zero-padding followed by 0x22
in little-endian encoding:
00 00 22 00 00 00
^^^^^ padding
^^^^^^^^^^^ 0x22
To fix both, specify the desired endianness explicitly:
struct.Struct('> B B I 15s I')
(The reason this gets rid of the padding is that struct
only pads structures when you use the default native encoding.)
Upvotes: 1