Page 1 of 1

Byte array as hex string

Posted: Sun Oct 01, 2017 12:29 pm
by mcauser
I have a byte array and when I print it, certain bytes are shown with ascii characters.
eg. bytearray(b'hello')

I'm trying to get it to display as bytearray(b'\x68\x65\x6c\x6c\x6f')

Is there a way to print it showing each byte in hex \xNN format?

Re: Byte array as hex string

Posted: Sun Oct 01, 2017 12:30 pm
by mcauser

Code: Select all

>>> data = bytearray(b'hello')
>>> print("".join("\\x%02x" % i for i in data))
\x68\x65\x6c\x6c\x6f
Close enough.