What are these extra characters in my byte strings?

Question:

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'x96x80Gx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'xb1xc1Gx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'xccx02Hx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'xe7CHx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'x02x85Hx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'x1dxc6Hx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'8x07Ix92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'SHIx92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'
b'nx89Ix92x00x00xc0x7fx00x00xc0x7fx00x00xc0x7fx00x00xc0x7f'

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.

Asked By: androman

||

Answers:

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.

Answered By: Code-Apprentice

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'x53x48x49x92x00'
>>> sensor_data
b'SHIx92x00'
>>> 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']

From Python 3.5 there is the hex() method on bytes to output a string object of hexadecimal digits. e.g.

>>> sensor_data.hex()
'5348499200'
>>> sensor_data.hex(' ')
'53 48 49 92 00'
Answered By: ukBaz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.