Home Linux Commandline Tips How to Convert Hex to ASCII Characters in Linux

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:

54657374696e672031203220330

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/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.

Ravi Saive
I am an Experienced GNU/Linux expert and a full-stack software developer with over a decade in the field of Linux and Open Source technologies. Founder of TecMint.com, LinuxShellTips.com, and Fossmint.com. Over 150+ million people visited my websites.

Each tutorial at UbuntuMint is created by a team of experienced writers so that it meets our high-quality standards.

Was this article helpful? Please add a comment to show your appreciation and support.

Got something to say? Join the discussion.

Thanks for choosing to leave a comment. Please keep in mind that all comments are moderated according to our comment policy, and your email address will NOT be published or shared. Please Do NOT use keywords in the name field. Let's have a personal and meaningful conversation.