问题
I have a Python 3 script that gets data from some Bluetooth LE sensors. The data I'm getting is in 20-byte strings, but it looks like there are some extra characters along with the typical \x00
bytes. What are these extra characters? Why are they in seemingly random spots in the string?
Here is a chunk of byte strings that I am getting from the sensors.
b'\x96\x80G\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'\xb1\xc1G\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'\xcc\x02H\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'\xe7CH\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'\x02\x85H\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'\x1d\xc6H\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'8\x07I\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'SHI\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
b'n\x89I\x92\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f\x00\x00\xc0\x7f'
For example, the first line has a chunk that says \x80G
. The second to last line starts with SHI
. What does this mean?
Thank you.
回答1:
As has been pointed out in the other answer, the SHI
is just an artifact of the binary values being printed to the screen.
A couple of options for printing the values so this doesn't happen are:
Python 3.7.3 (default, Dec 20 2019, 18:57:59)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> sensor_data = b'\x53\x48\x49\x92\x00'
>>> sensor_data
b'SHI\x92\x00'
>>> list(sensor_data)
[83, 72, 73, 146, 0]
>>> [hex(x) for x in sensor_data]
['0x53', '0x48', '0x49', '0x92', '0x0']
>>> [f'{x:02x}' for x in sensor_data]
['53', '48', '49', '92', '00']
>>>
回答2:
If the byte data is an ASCII character, then that character will be shown instead of the \xXX
escape code. So for example b'G'
is the same as b'\x47'
, b'S'
is the same as b'\x53'
, etc.
As for what this value actually means, I can't tell you. I'm not familiar with the protocols you are using here.
来源:https://stackoverflow.com/questions/64343864/what-are-these-extra-characters-in-my-byte-strings