How to convert from HEX to ASCII in Python [5 Ways]

avatar
Borislav Hadzhiev

Last updated: Apr 13, 2024
3 min

banner

# Table of Contents

  1. How to convert from HEX to ASCII in Python
  2. Convert from HEX to ASCII using binascii.unhexlify()
  3. Convert from HEX to ASCII using codecs.decode()
  4. Convert from HEX to ASCII by parsing and joining the characters

# How to convert from HEX to ASCII in Python

To convert from HEX to ASCII in Python:

  1. Use the bytearray.fromhex() method to get a new bytearray object that is initialized from the string of hex numbers.
  2. Use the decode() method to decode the bytearray.
  3. Optionally, set the encoding parameter to "ascii".
main.py
hex_str = '626f6262796861647a2e636f6d' result = bytearray.fromhex( hex_str ).decode() print(result) # ๐Ÿ‘‰๏ธ bobbyhadz.com

python convert hex to ascii

The code for this article is available on GitHub

By default, the encoding argument of the decode() method is set to utf-8, however, you can set it to ascii if necessary.

main.py
hex_str = '626f6262796861647a2e636f6d' result = bytearray.fromhex( hex_str ).decode(encoding='ascii') print(result)

setting the encoding argument to ascii

The bytearray.fromhex() method takes a string of hex numbers as a parameter and returns a new bytearray object.

main.py
hex_str = '626f6262796861647a2e636f6d' # ๐Ÿ‘‡๏ธ bytearray(b'bobbyhadz.com') print(bytearray.fromhex(hex_str))

Once we have a bytearray, we can use the bytes.decode() method.

main.py
hex_str = '626f6262796861647a2e636f6d' # ๐Ÿ‘‡๏ธ bobbyhadz.com print( bytearray.fromhex(hex_str).decode(encoding='ascii') )
The code for this article is available on GitHub

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.

main.py
hex_str = '626f6262796861647a2e636f6d' result = bytes.fromhex( hex_str ).decode() print(result) # ๐Ÿ‘‰๏ธ bobbyhadz.com

convert hex to ascii with bytes fromhex

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.

main.py
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.

# Convert from HEX to ASCII using binascii.unhexlify()

You can also use the binascii.unhexlify() method to convert from HEX to ASCII in Python.

main.py
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

convert from hex to ascii using binascii unhexlify

The code for this article is available on GitHub

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.

main.py
as_string = as_bytes.decode(encoding='ascii')

By default, the encoding argument is set to utf-8.

main.py
as_string = as_bytes.decode(encoding='utf-8')

# Convert from HEX to ASCII using codecs.decode()

You can also use the codecs.decode() method to convert from HEX to ASCII in Python.

main.py
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

convert from hex to ascii-using codecs decode

The code for this article is available on GitHub

The codecs.decode() method decodes the supplied object using the specified encoding.

The second argument we passed to the method is the encoding.

main.py
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.

main.py
as_string = as_bytes.decode(encoding='ascii')

# Convert from HEX to ASCII by parsing and joining the characters

You can also convert from HEX to ASCII in a more manual manner.

main.py
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

convert from hex to ascii by parsing and joining

The code for this article is available on GitHub

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.

main.py
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.

main.py
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.

main.py
result = ''.join( chr(int(''.join(char), 16)) for char in zip(hex_str[0::2], hex_str[1::2]) )
The code for this article is available on GitHub

The last step is to use the str.join() method to join the characters without a separator.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev