Convert Hex to ASCII Characters in Linux Command Line

Got a bunch of hexadecimal characters and want to convert them to a readable decimal system (ASCII)?

There are multiple ways to convert hex to ASCII in Linux. You may also use these methods in your shell script if required.

Converting Hexadecimal to ASCII in Linux

Hexadecimal is a number system in which you use a combination of numbers (0-9) to represent values ranging from 0 to 9 and alphabets (A-F) to represent values from 10 to 15.

It may sound complicated but it is still better than binary numbering where everything is 1 and 0.

Hex was extensively used in microcontrollers a few decades ago. You could still see it in use, especially with the Hex color pallet which is used to represent various shades of colors.

On the other hand, ASCII stands for American Standard Code for Information Interchange and has a completely different use case than Hexadecimal. ASCII is a method that is used to convert normal language characters to binary so computers can understand instructions from our side.

Through this guide, I will include various ways by which you can easily convert hex to ASCII.

Let's consider the following hex code (problem statement) which I want to convert:

4c696e757868616e64626f6f6b20313233

And if I succeed in converting the above hex code, the ASCII conversion should look like this:

Linuxhandbook 123

Method 1: Using xxd Command

xxd is a command-line utility that can create a hex dump from the given text and vice versa.

First I'm going to input hex string using echo command and will pair it with xxd using pipe.

echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p

Here, command option -r is used to convert hex code to ASCII, and -p is used to print the plain text.

sagar@itsfoss:~$ echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p 
Linuxhandbook 123sagar@itsfoss:~$ 

As you can see above, the output gets mixed with the prompt in the terminal because the converted output doesn't have the new line character. To make it more readable, you may append echo '' to the above command.

echo 4c696e757868616e64626f6f6b20313233 | xxd -r -p && echo ''
Converting hex to ASCII using xxd

Method 2: Using printf Command

Yes, you can convert hex to ASCII using the bash printf command. Here, I'm going to use the \x option which will take input of 1 or two digits as shown below:

printf '\x4c\x69\x6e\x75\x78\x68\x61\x6e\x64\x62\x6f\x6f\x6b\x20\x31\x32\x33'

There is no issue if you get a single digit at the end of the hex string and it will work fine as demonstrated.

Converting hex to ASCII using printf

Method 3: Using dc Command

You might be wondering how we can use deck calculator (dc) for converting hex to ASCII and I will show you exactly how.

echo "16i 4C696E757868616E64626F6F6B20313233 P" | dc

Here, 16i is used to indicate that you are dealing with hex radix, and P is used to print output from the dc command.

By using the above command, you will get a similar result as given below:

Converting hex to ASCII using dc

Method 4: Using Perl Command

So if your system has Perl installed, you can easily convert hex to ASCII using this method. Just interchange my hex code with yours in the given command and that's it.

echo 4c696e757868616e64626f6f6b20313233 | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' && echo ''

And you'll be met with the results as shown below:

Converting hex to ASCII using Perl

Method 5: Using sed Command

This method is a bit similar to the above but here you are using sed to filter and convert hex to ASCII using normal expression.

echo -n 4c696e757868616e64626f6f6b20313233 | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf && echo ''

By using piping, we collected hash string through echo, converted it by pairing sed to given expression, and got output as given.

Converting hex to ASCII using sed

Method 6: Convert hex strings stored in text file

So if you have a hex string stored under a text file, you can use this method to convert the stored hex string to ASCII.

My hex string is stored in a hex.txt file so to convert that string, I'll have to follow the given command:

cat hex.txt | xxd -r -p && echo ''

By far this is the most convenient way as you don't have to enter hex strings again and again and the best part is you can store multiple hex strings and convert them using the same command.

Converting hex to ASCII using hex file

This was my take on how you can convert hex string to ASCII using various methods. By far, the last one is my favorite as it is quite efficient compared to the others.

There are dedicated Hex editors in Linux and you may try them as well if you want.

Top 5 Hex Editors for Linux [GUI and CLI Tools]
Here are our choices for the best hex editors for Linux. The list includes both GUI and command line based Linux hex editors.