2

I'm trying to 'unpack' (using Python's struct module) a bytearray to multiple variables, using a formatted string:

(a, b, c, d, e, f, g, h) = unpack('HHHHHBBL', my_byte_array)

I expect (when I read the docs) that:

  • a through e will be an unsigned short (of a size 2 bytes each)
  • f and g will be an unsigned char (with a size of 1 byte each)
  • h will be an unsigned long (with a size of of 8 bytes)

When I run this on my Windows 10 machine, this is exactly what I get.

On my other two machines with Mac OS X and Manjaro Linux (all three have Python 3.7 installed), I will get an error stating:

struct.error: unpack requires a buffer of 24 bytes

When I run the following, the output is the same on all three machines

>>> from struct import *
>>> calcsize('H')
2
>>> calcsize('B')
1
>>> calcsize('L')
8

But when I run the following:

>>> calcsize('HHHHHBBL')

The Output on my Windows machine is 16, but on the other two systems 24. Which seems strange to me, what is going on here?

And how should I use struct.unpack in a multiplatform environment?

2
  • 3
    You have to start your struct format string with one of the standard byte order/size/alignment indicators (usually < or >) in order to get any sort of cross-platform compatibility. Commented Mar 14, 2019 at 13:57
  • Ah many thanks, that was it! :) @jasonharper. Can you add the comment as an answer, so I can mark this question as resolved? Commented Mar 14, 2019 at 14:07

1 Answer 1

1

Thanks @jasonharper:

You have to start your struct format string with one of the standard byte order/size/alignment indicators (usually < or >) in order to get any sort of cross-platform compatibility

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.