为什么是
struct.pack("!bbbb", 0x2, r, g, b)当r,g或b> 127时,我的python代码失败了吗?
我知道,根据struct,"b“表示给定值的大小为1字节,但是为什么超过127的值会失败呢?
发布于 2014-11-07 20:54:20
根据文献资料的说法,b代表:
签名字符
也就是说它的有效范围是-128,127。这就是错误信息明确指出的:
>>> struct.pack("!bbbb", 0x2, 127, 127, 128)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
struct.error: byte format requires -128 <= number <= 127使用B不会产生错误:
>>> struct.pack("!bbbB", 0x2, 127, 127, 128)
'\x02\x7f\x7f\x80'https://stackoverflow.com/questions/26809855
复制相似问题