Convert Hex to ASCII in Python

This tutorial shows the steps involved in converting HEXADECIMAL STRING to ASCII STRING in Python.


The first question that arises is, What is hexadecimal?

In our lower grades, we studied different number systems like the Binary system, Hexadecimal system, and others.

In this blog post, we look into the Hexadecimal system and ASCII. ASCII concept must be new to many of us. Both concepts shall be discussed below.


Hexa means 6 (six) and decimal means (10), both when summed up result in 16.
The highlighting feature of the Hexadecimal system is, that it has both numerical values and alphabets, it starts from 0 and ends at F. The 16 digits are,
0,1,2,3,4,5,6,7,8,9 A-10 B-11 C-12 D-13 E-14 and F-15. 


ASCII stands for American Standard Code for Information Interchange, developed by American National Standards Institute (ANSI). It is used to change computer language to human-understandable language. Only numbers are used in ASCII, for instance, the value of the letter “A” in ASCII is 65.

Now, we shall convert the hexadecimal value to ASCII value.

Note: In Python, we write Hexadecimal values with a prefix “0x”. For example, “0x4142”.
We use the following hexadecimal value to illustrate our example. 

Hexadecimal value:”0x48657920436F6465722E

ASCII value: Hey Coder.

Python program to convert Hex to ASCII


The program uses two functions “bytes.fromhex()” and “decode()“.






Run this code online

The output is as follows.

Output: Hey Coder. 

Explanation: Initially, we declare a variable and initialize it to a hexadecimal value. In the second step we  slice the given value, you can refer to How to slice a string in Python – Multiple way.
For the rest of the conversion, methods bytes.fromhex() and decode() are used.
In the output, Ascii value is displayed.

Leave a Reply

Your email address will not be published.