0

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

OrenIshShalom
  • 5,974
  • 9
  • 37
  • 87
  • 1
    Does this answer your question? [Print a string as hexadecimal bytes](https://stackoverflow.com/questions/12214801/print-a-string-as-hexadecimal-bytes) – Wouter Nov 23 '21 at 10:53

2 Answers2

1

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'
Abdul Niyas P M
  • 18,035
  • 2
  • 25
  • 46
1

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'
Daweo
  • 31,313
  • 3
  • 12
  • 25