How to Convert a String From Hex to ASCII in Python?

How to convert a string from hex to ASCII in python

Hex to ASCII conversion in Python | The hexadecimal number system is a 16-symbol numerical system. Hexadecimal is another name for the hexadecimal numeral system. This system employs decimal numbers (base 10) as well as six additional symbols (A to F). There are no numerical symbols in hexadecimal that represent values larger than nine.

What are the ASCII and Hexadecimal?

The American Standard Code for Information Interchange (ASCII) is an acronym for American Standard Code for Information Interchange. It was created by the American National Standards Institute (ANSI) and is used to transfer data from a high-level language to a low-level language. Only binary languages are understood by machines or computers. Integers are represented using the character data type. The ASCII value of the letter ‘A,’ for example, is 65.

In this tutorial, we’ll look at how to convert a hex string to an ASCII string in Python. The text is written in the hexadecimal form “0x68656c6c6f” and we want to convert it to an ASCII character string that will be hello since h equals 68 in ASCII code, e equals 65, l equals 6c, and o equals 6f.

As input, we’ll use a hexadecimal value string. Then extract all characters by converting hexadecimal value strings to their equivalent ASCII format strings. The ASCII string will be displayed on the screen at the end.

Converting Hex to ASCII in Python

You can also use the online hex to ASCII converter to transform your hexadecimal values into ASCII characters. Further, we have given the three different methods of conversion by using the python program.

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.

# Program to convert the hex string into the ascii code

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

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

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

Output: ASCII String: hello

2. Python Program to Convert From hex to ASCII

The codecs.decode(obj, encoding, error) method decodes an object using the encoding scheme supplied in the encoding parameter. In the event of an error, the error argument defines the error handling scheme to be utilized. The str() method converts the byte array returned to a string.

# 2nd Python program to convert hex string to ASCII

# 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

3. Python Program to convert the hex string to ASCII

The codecs.decode(obj, encoding, error) method decodes an object using the encoding scheme supplied in the encoding parameter. In the event of an error, the error argument defines the error handling scheme to be utilized. The str() method converts the byte array returned to a string.

# 3rd 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

Summary:

Now you are witnessed that this topic is not difficult. Once you grab the knowledge of this article, you can easily solve the problems related to this topic. Hex to ASCII is a pretty conceptual article.

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or do 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 *