I'm developing a Python3 program to process data from radar. In general, radar send data(hex numbers) to a port of my computer and I use socket to listen that port. And use code below to recive 4 bytes of data.
data = connection.recv(4)
print(data)
When testing my programm, radar send 08 00 00 01
and program print b'\x08\x00\x00\x01'
. I understand the '\x' means that the next to characters is a hex number, but I want to get numbers like [08, 00, 00, 01]
or a normal string from it. Tried the most obvious way:
strData = data.decode()
print(strData.split('\x'))
Failed with SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \xXX escape
.
And situation goes worse if radar send 08 0D 00 00
, print(data)
will print b'\x08\r\x00\x00'
, which makes me realized that string operations can not handle this problem.
So, what is the right way to convert bytes like b'\x08\x00\x00\x01'
to numbers like 08 00 00 01
.
String encoding drives me carzy -.- Although I am using Python3, solutions in Python2.7 is OK as well. Thanks in advance.