我想解壓一個C結構,以二進制形式交給我的Python程序,幷包含另一個嵌套結構。 C頭的相關部分看起來是這樣的:在Python中解壓縮嵌套的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.
如何爲此結構制定正確的格式字符串?
良好的漁獲物;我錯過了最後的量詞。隨着那個''.size'給出54。 –