How to Convert Hex to ASCII Characters in Linux

A numbering system is defined as hexadecimal if its representation is to base 16. The combined numerals and alphabetic characters that make up the hexadecimal system are:

0 1 2 3 4 5 6 7 8 9 A B C D E F. 

A hexadecimal numbering system is ideal for large digital systems as it can hold/represent long binary values. This system is referred to as base-16 because a combined total of 16 (digital and alphabetic) symbols from 0 to F are used to represent it.

In comparison to other numbering systems like decimal, hexadecimal provides a closer visual mapping making it easier to read ad interpret.

ASCII or American Standard Code for Information Interchange makes electronic communication possible through its character encoding standard. Therefore, this standard takes credit for text representation in devices like computers and telecommunication equipment.

For instance, to convert something like a user resume to ASCII format, that final document will be in plain text with no text formatting like underscores, bold, or tabs present. This raw format is easily processed by computers. It also makes it easy for the final document to be imported and/or used by other applications.

By the end of this article, we should be able to comfortably convert Hex to ASCII characters via Linux.

Problem Statement

Consider the following Hex characters:


Theoretically, the equivalent translation of the above Hex characters to ASCII characters is:

Testing 1 2 3 

We are going to look at various Linux-based approaches to arriving at the above-stated solution.

Method 1: Using echo and xxd Commands

We mainly use the xxd command to make a hexdump of sampled Hex characters or reverse the stated action. Firstly, an echo command will take the sample Hex characters as inputs before piping them to the xxd command for for conversion to ASCII characters.

$ echo 54657374696e672031203220330 | xxd -r -p && echo ''

The command option -r converts the Hex characters to ASCII and command option -p prints the outcome in plain text. The final echo command produces a newline on the Linux terminal.

Convert Hex to ASCII in Linux
Convert Hex to ASCII in Linux

If you wish to convert the Hex characters to ASCII characters from a file, use the cat command and pipe the file to xxd command.

$ cat sample_file.txt 
$ cat sample_file.txt | xxd -r -p && echo ''
Convert Hex to ASCII from File
Convert Hex to ASCII from File

Method 2: Using printf Command

The printf command formats and prints data input in respect to the specified data format. The command option \x takes Hex character inputs of 1 to 2 digits as demonstrated below:

$ printf '\x54\x65\x73\x74\x69\x6e\x67\x20\x31\x20\x32\x20\x33\x0' && echo ''

Testing 1 2 3 

Method 3: Using sed Command

The sed command is used to filter and transform the Hex characters input by passing it via a regular expression for conversion to ASCII character before the final output is printed.

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

Testing 1 2 3 

We can now comfortably convert simply complex Hex to ASCII characters from the Linux command-line environment.

Got something to say? Join the discussion.

Have a question or suggestion? Please leave a comment to start the discussion. Please keep in mind that all comments are moderated and your email address will NOT be published.