Reputation: 36353
Why is
struct.pack("!bbbb", 0x2, r, g, b)
failing in my python code when r, g, or b is > 127?
I know that the "b" means the size of a given value is 1 byte according to the struct docs, but why does it fail with values over 127?
Upvotes: 1
Views: 568
Reputation: 35891
According to the documentation, b
stands for:
signed char
which means its valid range is [-128, 127]. And that's what the error messages says explicitly:
>>> 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
Using B
yields no error:
>>> struct.pack("!bbbB", 0x2, 127, 127, 128)
'\x02\x7f\x7f\x80'
Upvotes: 4