Home > OS >  view bytes as hex values only (do not decode characters)
view bytes as hex values only (do not decode characters)

Time:11-24

How can I view a bytes array as hex values only?

x = b"\x61"
print(x)
b'a'

I really just want \x61 to be shown

CodePudding user response:

You might use binascii.hexlify if you are happy with just hex digits (no \x)

import binascii
x = b"\x61"
print(binascii.hexlify(x))

output

b'61'

CodePudding user response:

You can use int.from_bytes to get the integer represented by the given array of bytes. This integer can then passed into the hex function to get the hex value.

>>> import sys
>>>
>>> x = b"\x61"
>>> hex(int.from_bytes(x, sys.byteorder))
'0x61'
  • Related