Last updated: Apr 13, 2024
Reading timeยท3 min
To convert from HEX to ASCII in Python:
bytearray.fromhex()
method to get a new bytearray
object that is
initialized from the string of hex numbers.decode()
method to decode the bytearray
."ascii"
.hex_str = '626f6262796861647a2e636f6d' result = bytearray.fromhex( hex_str ).decode() print(result) # ๐๏ธ bobbyhadz.com
By default, the encoding
argument of the decode()
method is set to utf-8
,
however, you can set it to ascii
if necessary.
hex_str = '626f6262796861647a2e636f6d' result = bytearray.fromhex( hex_str ).decode(encoding='ascii') print(result)
The bytearray.fromhex()
method takes a string of hex numbers as a parameter
and returns a new bytearray
object.
hex_str = '626f6262796861647a2e636f6d' # ๐๏ธ bytearray(b'bobbyhadz.com') print(bytearray.fromhex(hex_str))
Once we have a bytearray
, we can use the
bytes.decode() method.
hex_str = '626f6262796861647a2e636f6d' # ๐๏ธ bobbyhadz.com print( bytearray.fromhex(hex_str).decode(encoding='ascii') )
The bytes.decode()
method decodes a bytes
object into a string literal.
The encoding
argument specifies the encoding with which to decode the
bytearray
.
There is also a bytes.fromhex
method that can be used to achieve the same
result.
hex_str = '626f6262796861647a2e636f6d' result = bytes.fromhex( hex_str ).decode() print(result) # ๐๏ธ bobbyhadz.com
The bytes.fromhex()
method returns a bytes
object that is decoded from the
supplied hex string.
The hex string parameter must contain two hexadecimal digits per byte where the ASCII whitespace is ignored.
The example above is equivalent to calling fromhex()
on a bytes
literal.
hex_str = '626f6262796861647a2e636f6d' result = b''.fromhex( hex_str ).decode() print(result) # ๐๏ธ bobbyhadz.com
When a string is prefixed with b
, it is a bytes object.
binascii.unhexlify()
You can also use the binascii.unhexlify() method to convert from HEX to ASCII in Python.
import binascii hex_str = '626f6262796861647a2e636f6d' as_bytes = binascii.unhexlify(hex_str) print(as_bytes) # ๐๏ธ b'bobbyhadz.com' as_string = as_bytes.decode(encoding='ascii') print(as_string) # ๐๏ธ bobbyhadz.com
The binascii.unhexlify()
method takes a hex string as a parameter and returns
the binary data represented by the hex string.
You can then use the bytes.decode()
method if you need to decode the bytes
into a string.
as_string = as_bytes.decode(encoding='ascii')
By default, the encoding
argument is set to utf-8
.
as_string = as_bytes.decode(encoding='utf-8')
codecs.decode()
You can also use the codecs.decode() method to convert from HEX to ASCII in Python.
import codecs hex_str = '626f6262796861647a2e636f6d' as_bytes = codecs.decode(hex_str, encoding='hex') print(as_bytes) # ๐๏ธ b'bobbyhadz.com' as_string = as_bytes.decode(encoding='ascii') print(as_string) # ๐๏ธ bobbyhadz.com
The codecs.decode()
method decodes the supplied object using the specified
encoding.
The second argument we passed to the method is the encoding.
as_bytes = codecs.decode(hex_str, encoding='hex')
By default, the encoding
argument is set to utf-8
.
If you need to get the result as a string, use the bytes.decode()
method.
as_string = as_bytes.decode(encoding='ascii')
You can also convert from HEX to ASCII in a more manual manner.
hex_str = '626f6262796861647a2e636f6d' result = ''.join( chr(int(''.join(char), 16)) for char in zip(hex_str[0::2], hex_str[1::2]) ) print(result) # ๐๏ธ bobbyhadz.com
The code sample converts from HEX to ASCII by parsing and joining the characters.
Notice that we passed 16
for the base
argument to the
int class because hex is a
positional numeral system that represents numbers using a base of 16.
print(int('6d', 16)) # ๐๏ธ 109 print(chr(109)) # ๐๏ธ m
The chr() function takes an integer that represents a Unicode code point and returns the corresponding character.
print(chr(int('6d', 16))) # ๐๏ธ
The example uses a generator expression to iterate over a
zip() object and uses the chr
and int
methods to convert each byte into an integer and then character.
result = ''.join( chr(int(''.join(char), 16)) for char in zip(hex_str[0::2], hex_str[1::2]) )
The last step is to use the str.join() method to join the characters without a separator.
You can learn more about the related topics by checking out the following tutorials: