How to switch byte order of binary data

pfyon picture pfyon · Nov 2, 2011 · Viewed 13.4k times · Source

I have a binary file which I'm reading where some 2 byte values are stored in 'reverse' byte order (little endian?), eg.

1D 00 13 00 27  00 3B 00 45  00 31 00 4F

The original program that created these values stores them internally as shorts. Those values should correspond to: 29, 19, 39, 59, 69, 49, 79. I'm trying to read these values using python 2.6.5 (although this will probably be run on much older systems, eg 2.3 and 2.4).

I've tried using

val1, val2, val3, val4, val5, val6, val7 = struct.unpack("1h 1h 1h 1h 1h 1h 1h", data)

and, of course, the values all come out wrong:

7427
4864
9984
15104
17664
12544
20224

After looking at the documentation for struct, I thought I'd be able to use something like

val1, ... = struct.unpack("!h !h ...

but when testing, I only got

struct.error: bad char in struct format

How can I unpack these values with the correct byte ordering? Am I stuck reading in the two bytes separately then reassembling them in the python code?

Answer

Cat Plus Plus picture Cat Plus Plus · Nov 2, 2011

Byte order is specified with a single character, at the beginning of the format string.

values = struct.unpack('!7h', data)