2

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...

1 Answer 1

1

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.)

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.