Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/332.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/sockets/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python struct.pack()因int而失败>;127_Python_Sockets_Struct.pack - Fatal编程技术网

Python struct.pack()因int而失败>;127

Python struct.pack()因int而失败>;127,python,sockets,struct.pack,Python,Sockets,Struct.pack,为什么 当r、g或b>127时python代码失败 我知道,根据struct docs,“b”表示给定值的大小为1字节,但为什么它在值超过127时失败呢?根据,b代表: 签名字符 这意味着它的有效范围是[-128127]。这就是错误消息明确指出的: struct.pack("!bbbb", 0x2, r, g, b) >>> struct.pack("!bbbb", 0x2, 127, 127, 128) Traceback (most recent call last):

为什么

当r、g或b>127时python代码失败

我知道,根据struct docs,“b”表示给定值的大小为1字节,但为什么它在值超过127时失败呢?

根据,
b
代表:

签名字符

这意味着它的有效范围是[-128127]。这就是错误消息明确指出的:

struct.pack("!bbbb", 0x2, r, g, b)
>>> 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
>>> struct.pack("!bbbB", 0x2, 127, 127, 128)
'\x02\x7f\x7f\x80'