What are these extra characters in my byte strings?

独自空忆成欢 提交于 2021-01-29 09:12:29

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!