【发布时间】:2013-08-07 13:54:44
【问题描述】:
我正在尝试解压缩以二进制形式传递给我的 Python 程序并包含另一个嵌套结构的 C 结构。 C 标头的相关部分如下所示:
typedef struct {
uint8_t seq;
uint8_t type;
uint16_t flags;
uint16_t upTimestamp;
}__attribute__ ((packed)) mps_packet_header;
typedef struct {
mps_packet_header header;
int16_t x[6];
int16_t y[6];
int16_t z[6];
uint16_t lowTimestamp[6];
}__attribute__((packed)) mps_acc_packet_t;
typedef mps_acc_packet_t accpacket_t;
现在,在我的 Python 程序中,我想使用 struct.unpack 解压缩 accpacket。但是,我不知道解包的格式字符串应该是什么,因为accpacket 包含嵌套的mps_packet_header。我尝试在开头插入mps_packet_header 的格式字符串,然后继续使用accpacket 的其余部分:
s = struct.Struct('= B B H H 6h 6h 6h H')
seq, _type, flags, upTimestamp, x, y, z, lowTimestamp = s.unpack(packet_data)
但是,这显然是不正确的;格式字符串的 calcsize 为 44,而结构本身的大小为 54。
如何为这个结构制定正确的格式字符串?
【问题讨论】:
标签: python struct nested unpack