开发者

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

开发者 https://www.devze.com 2023-03-16 00:05 出处:网络
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 t

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 m开发者_开发知识库ore 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,)


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)
0

精彩评论

暂无评论...
验证码 换一张
取 消