I have some data from Serial Port as follows
list_input =[ '\xbb', '\x02', '\x00', '\x11', '\xbe', '\x04', '\x00', '\x0', '\x08', '\x3', '\xb2', '\xdd', '\xd9', '\x01', '\x00', '\x00', '\x00', '\x00', '\xc4', '\x1e']
I want to remove '\\x' from each element and get output like,
list_output=[bb,02,00,22,be,04,00,08,dd]
if i do this list_input =''.join(map(str, list_input))
i get this output " " 4 3 @ " ie garbage value.
Please suggest any suitable solution.
The strings \\xbb
, \\x02
ecc are single character strings. What you are seeing is the hex escape representation of them, since the ASCII character with code 2
is not a printable character.
It seems like you actually want the base-16 representation of the number represented by this characters, without the x
prefix, hence you can us e ord
to obtain the ASCII value and then hex
to convert it into its hexadecimal representation:
>>> ord('\x02')
2
>>> ord('\xbb')
187
>>> hex(2)
'0x2'
>>> hex(187)
'0xbb'
If you don't want the 0x
prefix oyu can just use slicing to remove that part:
result = [hex(ord(x))[2:] for x in list_input]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.