Difference between '\x81' and 0x81
题
I'm new to Python and I'm trying to read some values from an USB device via PyUSB. Well, it works now but I ran into some trouble: While reading data from the device, PyUSB needs an endpoint to read the data from. This endpoint is identified via a hex value. If I read the data like...
dev.read('\x81', ...)
... I get an error "AttributeError: 'NoneType' object has no attribute 'bmAttributes'". If I read the data like...
dev.read(0x81, ...)
... it works.
So my simple question is: What's the difference between 0x81 and '\x81'?
:-)
解决方案
In Python 2.x, '\x81'
is a bytestring that consists of a single byte with value 129. You can also get it by calling chr(129)
.
0x81
is an integer written in base 16, with the value 129. You can also get it by simply writing 129
.
其他提示
\x81 correspond to an unicode caracters the one with 81 hexcode, 0x81 is a valid hexadecimal value.
The first one is a string with a particular hex value forced into the first character position, the second one is an integer.
Apparently that API call definitely expects an integer.
0x81 is sending in a hexadecimal number which it asks for, while '\x81' is sending in a string of characters. That is why the second one works, hope that helps and is actually what you were asking :)