Question

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'?

:-)

Was it helpful?

Solution

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.

OTHER TIPS

\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 :)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top