4

I'm having trouble using the struct.pack() for packing an integer.

With

struct.pack("BIB", 1, 0x1234, 0) 

I'm expecting

'\x01\x00\x00\x034\x12\x00'

but instead I got

'\x01\x00\x00\x004\x12\x00\x00\x00'

I'm probably missing something here. Please help.

2 Answers 2

8
'\x01\x00\x00\x004\x12\x00\x00\x00'
                 ^ this '4' is not part of a hex escape

is actually the same as:

'\x01\x00\x00\x00\x34\x12\x00\x00\x00'

Because the ASCII code for "4" is 0x34.

Because you used the default (native) format, Python used native alignment for the data, so the second field was aligned to offset 4 and 3 zeroes were added before it.

To get a result more like what you wanted, use the format >BIB or <BIB (for big-endian or little-endian respectively) This gives you '\x01\x00\x00\x12\x34\x00' or '\x01\x34\x12\x00\x00\x00'. Neither of those are exactly what you specified, because the example you gave was not proper big-endian or little-endian representation of 0x1234.

See also: section Byte Order, Size, and Alignment in the documentation.

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

Comments

2

From the docs

Note By default, the result of packing a given C struct includes pad bytes in order to maintain proper alignment for the C types involved; similarly, alignment is taken into account when unpacking. This behavior is chosen so that the bytes of a packed struct correspond exactly to the layout in memory of the corresponding C struct. To handle platform-independent data formats or omit implicit pad bytes, use standard size and alignment instead of native size and alignment: see Byte Order, Size, and Alignment for details.

You can get your desired result by forcing the byte order. (chr(0x34) == '4')

>>> struct.pack(">BIB", 1, 0x1234, 0)
'\x01\x00\x00\x124\x00'

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.