I’m using PySerial (Python 2.7) to read information from a device like this:
buffer += ser.read(3)
Now I have three bytes in buffer (i.e. 0xAE0259) which is of type str. Since I’m new to Python, I’m looking for the “pythonian” way to cut off the left most (0xAE) of the three bytes and then interpret the remaining two as int.
First I thought of a bit mask: buffer &= 0xFFFF
but python won’t let me use bit operators on str.
Any attempt to convert buffer to int failed as well.
Then I read about the ‘bitstring module’ which let’s me slice ranges of bits out of a BitArray, but I guess that using it for this would be a little over the top?
You need to know whether multi-byte types are big or little endian and whether it’s signed or unsigned. Assuming the two bytes are an unsigned, big-endian short, I would do the following:
‘>’ means big endian. ‘B’ is the first unsigned byte, which you don’t want. ‘H’ is an unsigned short.