Skip to content
Advertisement

How to print hex to the screen in python

I am trying to get python to print a hex string to the screen and not convert it to the ascii character.

>>> x=b'x5ex2ex6d'
>>> x
'^.m'

Is there a way to print this to the screen as 'x5ex2ex6d' instead of '^.m'

Any help will be greatly appreciated.

Advertisement

Answer

b'x5ex2ex6d' and b'^.m' are identical as far as Python is concerned. However, you can format it as you desire like this:

>>> x = b'x5ex2ex6d'
>>> print(''.join(map(r'x{:x}'.format, bytearray(x))))
x5ex2ex6d

or in Python3

>>> print(''.join([r'x{:x}'.format(c) for c in x]))
x5ex2ex6d

or in Python2

>>> print(''.join([r'x{:x}'.format(ord(c)) for c in x]))
x5ex2ex6d
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement