The int("0x31", 16) part is correct:
>>> int("0x31",16)
49
But to convert that to a character, you should use the chr(...) function instead:
>>> chr(49)
'1'
Putting both of them together (on the first letter):
>>> chr(int("0x53", 16))
'S'
And processing the whole list:
>>> [chr(int(i, 16)) for i in "0x53 0x48 0x41 0x53 0x48 0x49".split()]
['S', 'H', 'A', 'S', 'H', 'I']
And finally turning it into a string:
>>> hex_string = "0x53 0x48 0x41 0x53 0x48 0x49"
>>> ''.join(chr(int(i, 16)) for i in hex_string.split())
'SHASHI'
I hope this helps!
Answer from filbranden on Stack OverflowHow to store Hex and convert Hex to ASCII in Python? - Stack Overflow
How do I convert a single character into its hex ASCII value in Python? - Stack Overflow
Convert Hex to ascii
Converting string to ASCII HEX Python 3
The int("0x31", 16) part is correct:
>>> int("0x31",16)
49
But to convert that to a character, you should use the chr(...) function instead:
>>> chr(49)
'1'
Putting both of them together (on the first letter):
>>> chr(int("0x53", 16))
'S'
And processing the whole list:
>>> [chr(int(i, 16)) for i in "0x53 0x48 0x41 0x53 0x48 0x49".split()]
['S', 'H', 'A', 'S', 'H', 'I']
And finally turning it into a string:
>>> hex_string = "0x53 0x48 0x41 0x53 0x48 0x49"
>>> ''.join(chr(int(i, 16)) for i in hex_string.split())
'SHASHI'
I hope this helps!
>>> import binascii
>>> s = b'SHASHI'
>>> myWord = binascii.b2a_hex(s)
>>> myWord
b'534841534849'
>>> binascii.a2b_hex(myWord)
b'SHASHI'
>>> bytearray.fromhex("534841534849").decode()
'SHASHI'
There are several ways of doing this:
>>> hex(ord("c"))
'0x63'
>>> format(ord("c"), "x")
'63'
>>> import codecs
>>> codecs.encode(b"c", "hex")
b'63'
On Python 2, you can also use the hex encoding like this (doesn't work on Python 3+):
>>> "c".encode("hex")
'63'
This might help
import binascii
x = b'test'
x = binascii.hexlify(x)
y = str(x,'ascii')
print(x) # Outputs b'74657374' (hex encoding of "test")
print(y) # Outputs 74657374
x_unhexed = binascii.unhexlify(x)
print(x_unhexed) # Outputs b'test'
x_ascii = str(x_unhexed,'ascii')
print(x_ascii) # Outputs test
This code contains examples for converting ASCII characters to and from hexadecimal. In your situation, the line you'd want to use is str(binascii.hexlify(c),'ascii').
Hi all,
I've been struggling with this for an hour or so, I'm trying to take a string such as
'Hell' to '\x48\x65\x6c\x6c'
For what I'm doing, I require the \x before the ascii hex
This is a dated solution from StackOverflow, that doesn't work on Python3
>>> mystring = "Hello World"
>>> print ''.join(r'\x{02:x}'.format(ord(c)) for c in mystring)
\x48\x65\x6c\x6c\x6f\x20\x57\x6f\x72\x6c\x64 My new solution goes like this:
mystring = b'hello world'
#convert to hex
mystring = hexlify(mystring)
mystring = mystring.decode('ascii')
# hex, like this 68656c6c6f20776f726c64
#add in the \x in the right spots
mystring = '\\x'.join(mystring[i:i + 2] for i in range(-2, len(mystring), 2))
#looks like this at this point \x68\x65\x6c\x6c\x6f\x20\x77\x6f\x72\x6c\x64
#I need it back into the "b" format, so I encode it
mystring=mystring.encode(ascii)
# This breaks everything, now it becomes:
# b'\\x68\\x65\\x6c\\x6c\\x6f\\x20\\x77\\x6f\\x72\\x6c\\x64'
# I specifically can't use the double backslashes.Any ideas how to fix this or am I going about this all wrong? Thanks