0

There seems to be some ordering issues when using format strings in Python.

For example, I am trying to unpack, in order: 3 doubles, 1 float, 2 doubles. This should be 5*8+4 == 44 bytes in size.

Consider the following code:

import struct

A = struct.calcsize("dddfdd")
B = struct.calcsize("dddf")+struct.calcsize("dd")
C = struct.calcsize("dddddf")

For me, in Python 3.6.9 on Linux, I get that A == 48, meanwhile B == C == 44 (as I would expect).

This means that something like the following would fail, when packed_data is 44 bytes.

unpacked_data = struct.unpack('dddfdd',packed_data)

I can, instead, resort to:

unpacked_data1 = struct.unpack('dddf',packed_data1)
unpacked_data2 = struct.unpack('dd',packed_data2)

What gives? Is there a better way to do the unpacking?

1

1 Answer 1

1

Try to specify a byte order with your format string:

# assumes a native byte order, size and alignment.
# This means for alignment purposes, that it will align bytes to multiples of 8 bytes
A = struct.calcsize("dddfdd")

# if you prepend '=' infront of the format string, the order assumes system byte order and no alignment
A = struct.calcsize("=dddfdd") 
# A == 44

To check your system byteorder you can also use:

import sys
print(sys.byteorder)

As reference look here

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

2 Comments

I see, thanks. Is it padding it to 8 bytes because I have a 64 bit system?
Yes, that is correct. For more information look at e.g. the Wikipedia entry.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.