Convert Hex String to ASCII String in Python

Python hex to ASCII | The hexadecimal number system is a numeral system made up of 16 symbols. The hexadecimal number system is also known as hex. This system uses the decimal numbers (base 10) and six extra symbols (A to F). In hexadecimal no numerical symbols that represent If values greater than nine.

ASCII stands for American Standard Code for Information Interchange. It was developed by the ANSI (American National Standards Institute) and it is used to interchange the information from a high-level language to low-level language. Machine or Computer understand only binary languages. So, the character data type represents integers. For example, the ASCII value of the letter ‘A’ is 65.

We will discuss how to convert hex string to ASCII string in python. The string is written in hexadecimal form “0x68656c6c6f” 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 65, l is 6c and o is 6f.

We will take a hexadecimal value string as input. Then, convert hexadecimal value string to their corresponding ASCII format string and extract all characters. Finally, the ASCII string will be displayed on the screen.

Python Program to Convert Hex to ASCII

The bytes.fromhex() function convert hex to the byte in python. This function accepts a single hexadecimal value argument and converts it into a byte array first. The decode() method takes a byte array as input and decodes it. Use the slicing notation hex_str[2:] to remove “0x” from a hexadecimal string.

# Python program to convert hex string to ASCII string

# take hexadecimal string
hex_str = "0x68656c6c6f"[2:]

# convert hex string to ASCII string
bytes_array = bytes.fromhex(hex_str)
ascii_str = bytes_array.decode()

# printing ASCII string
print('ASCII String:', ascii_str)

Output:-

ASCII String: hello

Hex to ASCII in Python

The codecs.decode(obj, encoding, error) method 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. The str() method is to convert the returned byte array to string.

# Python program to convert hex string to ASCII string

# importing codecs.decode()
import codecs

# take hexadecimal string
hex_str = '0x68656c6c6f'[2:]

# convert hex string to ASCII string
binary_str = codecs.decode(hex_str, 'hex')
ascii_str = str(binary_str,'utf-8')

# printing ASCII string
print('ASCII String:', ascii_str)

Output:-

ASCII String: hello

Python Program to Convert Hex to String

In the previous program, we used the built-in function to convert hexadecimal to string but, in this program, we are using the native method to convert hexadecimal to string.

# Python program to convert hex string to ASCII string

# take hexadecimal string
hex_str = '0x68 0x65 0x6c 0x6c 0x6f'

# convert hex string to ASCII string
string = ''.join(chr(int(i, 16)) for i in hex_str.split())

# printing ASCII string
print('ASCII String:', string)

Output:-

ASCII String: hello

Also See:- Hexadecimal to Decimal in Python

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Comment

Your email address will not be published. Required fields are marked *