1

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?

Abhijit
  • 1,728
  • 1
  • 14
  • 25
  • 1
    Can you share your code. – planet260 Mar 03 '15 at 07:41
  • http://stackoverflow.com/questions/17898730/python-readline see if it helps – planet260 Mar 03 '15 at 07:47
  • I tried with data \x06\x02\x41\x42\x4C\x00\x00\x00\x02 and used code with open("D:\\Test.txt", 'r') as f: print f.readline() it returned me correct result i-e \x06\x02\x41\x42\x4C\x00\x00\x00\x02 – planet260 Mar 03 '15 at 07:49
  • Please avoid using code in comments, edit your answer instead – gboffi Mar 03 '15 at 07:54
  • I am using a terminal which sends byte that content hex data. I am reading using serial readline command and the when I do that , this is what I get: b'\x06\x02ABL\x00\x00\x00\x02 – Abhijit Mar 03 '15 at 08:46

1 Answers1

2

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'
jfs
  • 399,953
  • 195
  • 994
  • 1,670