Search code examples
pythonmultiplatform

Differences in Python's packed binary data size among platforms?


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?


Solution

  • 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