Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I unpack a float with other types and get the expected result?

Tags:

python

struct

I'm trying to parse out some data packed into this binary file and Python's struct module is causing me all sorts of problems. It won't seem to give me the correct float variable when it's trying to do more than one type at a time:

import struct

# a fragment of the binary file
a = '\x39\x00\xFF\x00\x00\x0A\x00\x1F\x05\xDC\x42\x31\x30\x00\xFF\x00\x00\x0A\x00\xB5\x01\xE6\x42'

struct.unpack_from('1sxHxbxf', a)
# returns ('9', 255, 10, 2.8355782166755716e-09), but 
struct.unpack_from('f',a[7:])
# gives the expected (110.01000213623047,)
like image 604
Nick T Avatar asked Feb 23 '23 18:02

Nick T


1 Answers

By default, C types are represented in the machine’s native format and byte order, and properly aligned by skipping pad bytes if necessary (according to the rules used by the C compiler).

The unpacking expects the float to be aligned on an 8-byte boundary and skips over 1 padding byte to get there. You can confirm this by skipping 1 byte yourself:

>>> struct.unpack_from('1sxHxbxf', a)
('9', 255, 10, 2.8355782166755716e-09)
>>> struct.unpack_from('f',a[8:])
(2.8355782166755716e-09,)

To disable alignment, add =, <, >, or ! to the front of the format string.

>>> struct.unpack_from('=1sxHxbxf', a)
('9', 255, 10, 110.01000213623047)
like image 81
John Kugelman Avatar answered Apr 29 '23 06:04

John Kugelman