Convert Hex to ASCII in Python

Convert Hex to ASCII in Python

  1. Convert Hex to ASCII in Python Using the decode() Method
  2. Convert Hex to ASCII in Python Using the codecs.decode() Method

This tutorial will look into various methods to convert a hexadecimal string to an ASCII string in Python. Suppose we have a string written in hexadecimal form 68656c6c6f and we want to convert it into an ASCII character string which will be hello as h is equal to 68 in ASCII code, e is 64, l is 6c and o is 6f.

We can convert a hexadecimal string to an ASCII string in Python using the following methods:

Convert Hex to ASCII in Python Using the decode() Method

The string.decode(encoding, error) method in Python 2 takes an encoded string as input and decodes it using the encoding scheme specified in the encoding argument. The error parameter specifies the error handling schemes to use in case of an error that can be strict, ignore, and replace.

Therefore, to convert a hex string to an ASCII string, we need to set the encoding parameter of the string.decode() method as hex. The below example code demonstrates how to use the string.decode() method to convert a hex to ASCII in Python 2.

string = "68656c6c6f"
string.decode("hex")

Output:

hello

In Python 3, the bytearray.decode(encoding, error) method takes a byte array as input and decodes it using the encoding scheme specified in the encoding argument.

To decode a string in Python 3, we first need to convert the string to a byte array and then use the bytearray.decode() method to decode it. The bytearray.fromhex(string) method can be used to convert the string into a byte array first.

The below example code demonstrates how to use the bytearray.decode() and bytearray.fromhex(string) method to convert a hex string to ASCII string in Python 3:

string = "68656c6c6f"
byte_array = bytearray.fromhex(string)
byte_array.decode()

Output:

hello

Convert Hex to ASCII in Python Using the codecs.decode() Method

The codecs.decode(obj, encoding, error) method is similar to decode() method. It takes an object as input and decodes it using the encoding scheme specified in the encoding argument. The error argument specifies the error handling scheme to be used in case of an error.

In Python 2, the codecs.decode() returns a string as output, and in Python 3, it returns a byte array. The below example code demonstrates how to convert a hex string to ASCII using the codecs.decode() method and convert the returned byte array to string using the str() method.

import codecs

string = "68656c6c6f"
binary_str = codecs.decode(string, "hex")
print(str(binary_str,'utf-8'))

Output:

hello

Related Article - Python ASCII

  • Convert String to ASCII Value in Python
  • Convert Int to ASCII in Python
  • Get ASCII Value of a Character in Python
  • Related Article - Python Hex

  • Bitwise XOR of Hex Numbers in Python
  • Convert Binary to Hex in Python
  • Convert HEX to RGB in Python
  • String to Hex in Python
  • Convert Hexadecimal to Decimal in Python