Abhijit
Abhijit

Reputation: 1856

Python prevent readline from converting to hex value

I am using readline command and getting hex values. However when I print the data received it converts few of the hex values to characters. Example:

b'\x06\x02ABL\x00\x00\x00\x02'
#         ^^^

I would like it to be displayed as

b'\x06\x02\x41\x42\x4C\x00\x00\x00\x02'

How I can achieve this?

Upvotes: 1

Views: 1420

Answers (1)

jfs
jfs

Reputation: 414585

What you see is the representation of a bytestring in Python e.g.:

>>> b'\x30\x31\x00'
b'01\x00'

It shows printable bytes as their ascii symbols instead of the corresponding hex escapes.

I am using readline command and getting hex values.

"hex values" is just a sequence of bytes (numbers in range 0 to 255) here.

If you want to display all bytes as the corresponding hex values:

>>> import binascii
>>> binascii.hexlify(b'01\x00')
b'303100'

Upvotes: 2

Related Questions