struct.error: unpack 需要长度为 4 的字符串参数 [英] struct.error: unpack requires a string argument of length 4

查看:77
本文介绍了struct.error: unpack 需要长度为 4 的字符串参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Python 说我需要 4 个字节来表示BH"的格式代码:

struct.error: unpack 需要长度为 4 的字符串参数

这是代码,我认为需要输入 3 个字节:

major, minor = struct.unpack("BH", self.fp.read(3))

"B" Unsigned char (1 byte) + "H" Unsigned short (2 bytes) = 3 bytes (!?)

struct.calcsize("BH") 表示 4 个字节.

文件大小约为 800 MB,位于文件的前几个字节中,因此我很确定还有数据要读取.

解决方案

struct 模块模仿 C 结构.处理器读取奇数地址上的 16 位字或不能被 4 整除的地址上的 32 位双字需要更多 CPU 周期,因此结构添加填充字节"以使结构成员落在自然边界上.考虑:

struct { 11字符一个;012345678901短 b;------------字符 c;axbbcxxxdddd国际d;};

此结构将占用 12 个字节的内存(x 为填充字节).

Python 的工作原理类似(请参阅 struct 文档):

<预><代码>>>>导入结构>>>struct.pack('BHBL',1,2,3,4)'\x01\x00\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00'>>>struct.calcsize('BHBL')12

编译器通常有一种消除填充的方法.在 Python 中,任何 =<>!将消除填充:

<预><代码>>>>struct.calcsize('=BHBL')8>>>struct.pack('=BHBL',1,2,3,4)'\x01\x02\x00\x03\x04\x00\x00\x00'

当心让结构处理填充.在 C 中,这些结构:

struct A { struct B {短一个;一个;字符 b;字符 b;};};

通常分别为 4 和 8 个字节.如果在数组中使用结构,则填充发生在结构的末尾.这使 'a' 成员在数组后面的结构的正确边界上对齐.Python 的 struct 模块最后没有填充:

<预><代码>>>>struct.pack('LB',1,2)'\x01\x00\x00\x00\x02'>>>struct.pack('LBLB',1,2,3,4)'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04'

Python says I need 4 bytes for a format code of "BH":

struct.error: unpack requires a string argument of length 4

Here is the code, I am putting in 3 bytes as I think is needed:

major, minor = struct.unpack("BH", self.fp.read(3))

"B" Unsigned char (1 byte) + "H" Unsigned short (2 bytes) = 3 bytes (!?)

struct.calcsize("BH") says 4 bytes.

EDIT: The file is ~800 MB and this is in the first few bytes of the file so I'm fairly certain there's data left to be read.

解决方案

The struct module mimics C structures. It takes more CPU cycles for a processor to read a 16-bit word on an odd address or a 32-bit dword on an address not divisible by 4, so structures add "pad bytes" to make structure members fall on natural boundaries. Consider:

struct {                   11
    char a;      012345678901
    short b;     ------------
    char c;      axbbcxxxdddd
    int d;
};

This structure will occupy 12 bytes of memory (x being pad bytes).

Python works similarly (see the struct documentation):

>>> import struct
>>> struct.pack('BHBL',1,2,3,4)
'\x01\x00\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00'
>>> struct.calcsize('BHBL')
12

Compilers usually have a way of eliminating padding. In Python, any of =<>! will eliminate padding:

>>> struct.calcsize('=BHBL')
8
>>> struct.pack('=BHBL',1,2,3,4)
'\x01\x02\x00\x03\x04\x00\x00\x00'

Beware of letting struct handle padding. In C, these structures:

struct A {       struct B {
    short a;         int a;
    char b;          char b;
};               };

are typically 4 and 8 bytes, respectively. The padding occurs at the end of the structure in case the structures are used in an array. This keeps the 'a' members aligned on correct boundaries for structures later in the array. Python's struct module does not pad at the end:

>>> struct.pack('LB',1,2)
'\x01\x00\x00\x00\x02'
>>> struct.pack('LBLB',1,2,3,4)
'\x01\x00\x00\x00\x02\x00\x00\x00\x03\x00\x00\x00\x04'

这篇关于struct.error: unpack 需要长度为 4 的字符串参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆