我的字节字符串中这些多余的字符是什么? [英] What are these extra characters in my byte strings?

查看:81
本文介绍了我的字节字符串中这些多余的字符是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Python 3脚本,该脚本从一些Bluetooth LE传感器获取数据.我得到的数据是20字节的字符串,但看起来好像还有一些额外的字符以及典型的 \ x00 字节.这些多余的字符是什么?为什么它们会出现在字符串中看似随机的位置?

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'

例如,第一行有一个大块,上面写着 \ x80G .倒数第二行以 SHI 开头.这是什么意思?

For example, the first line has a chunk that says \x80G. The second to last line starts with SHI. What does this mean?

谢谢.

推荐答案

正如其他答案中指出的那样, SHI 只是打印到屏幕上的二进制值的伪像.

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

这篇关于我的字节字符串中这些多余的字符是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆