I am trying to get python to print a hex string to the screen and not convert it to the ascii character.
JavaScript
x
4
1
>>> x=b'x5ex2ex6d'
2
>>> x
3
'^.m'
4
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:
JavaScript
1
4
1
>>> x = b'x5ex2ex6d'
2
>>> print(''.join(map(r'x{:x}'.format, bytearray(x))))
3
x5ex2ex6d
4
or in Python3
JavaScript
1
3
1
>>> print(''.join([r'x{:x}'.format(c) for c in x]))
2
x5ex2ex6d
3
or in Python2
JavaScript
1
3
1
>>> print(''.join([r'x{:x}'.format(ord(c)) for c in x]))
2
x5ex2ex6d
3