kangtinglee
kangtinglee

Reputation: 32

Different behavior of struct.pack()

When I tried to run this same program on my computer and my school's server, I get these two different behaviors from struct.pack(...).

This is from my computer

Python 3.7.0 (default, Oct  9 2018, 10:31:47) 
[GCC 7.3.0] :: Anaconda, Inc. on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.pack('HL',0,123456)
b'\x00\x00\x00\x00\x00\x00\x00\x00@\xe2\x01\x00\x00\x00\x00\x00'

This is from my school server

Python 3.7.0 (default, Aug  1 2018, 14:55:42) 
[GCC 4.8.4] on sunos5
Type "help", "copyright", "credits" or "license" for more information.
>>> import struct
>>> struct.pack('HL',0,123)
b'\x00\x00\x00\x00\x00\x00\x00{'

As you can see, the length of the output is different on both systems, for reasons unrelated to Python version. Is there a way to coerce or force the output to be 8 or 16 bytes long? The HL format is actually only 6 bytes long, but on the school server, it expands to be 8 bytes. On my local computer, 'HL' expands to 16 bytes for some reason.

This behavior is critical because I need to pass this function later on to struct.unpack(...) which would require different length inputs on depending on the length of the output from struct.pack(...).

Upvotes: 0

Views: 184

Answers (1)

Guillaume Jacquenot
Guillaume Jacquenot

Reputation: 11717

You are running the same code on two different machines, that interprets the format to pack differently depending on 32 or 64 bit os you are using.

On a 32 and on a 64 bit architectures, the sizes of the fundamental types differ, which produces different length. Twice the second on the 64 bit architecture for your particular example.

Please have a look at this page for a better understanding.

My bet is that your are executing the 1st code on a 64 bit machine, and the second on a 32 bit machine.

You can run this code to determine whether you are using a 32 or a 64 bit architecture. See here for details

python -c 'import struct;print( 8 * struct.calcsize("P"))'

It should produce 64 on the 1st machine and 32 on the second.

In fact, if you pack and unpack data on the same machine, you won't have any problem. But as you start sending binary data from a machine to another without any precaution, you will encounter problems. (32/64 problem as you have met, endianess). This is why you need to describe data encoding when working with multiple machines. HDF5 is file data storage that can help you manage all these difficulties.

Upvotes: 1

Related Questions