- Data Structure
- Networking
- RDBMS
- Operating System
- Java
- MS Excel
- iOS
- HTML
- CSS
- Android
- Python
- C Programming
- C++
- C#
- MongoDB
- MySQL
- Javascript
- PHP
- Physics
- Chemistry
- Biology
- Mathematics
- English
- Economics
- Psychology
- Social Studies
- Fashion Studies
- Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Convert Hex to ASCII Characters in the Linux Shell
Introduction
If a numbering system is represented in base 16 then it is considered to be hexadecimal. In this article we will understand to convert Hex to ASCII in the Linux Shell. The hexadecimal system uses a combination of digits and alphabetic characters, and it looks like this −
0 1 2 3 4 5 6 7 8 9 A B C D E F
Large digital systems are best suited for a hexadecimal numbering system since it can store and express long binary data. Because a total of 16 symbols (both digital and alphabetic, ranging from 0 to F) are used to represent this system, it is known as base16.
Through its character encoding standard, ASCII, or the American Standard Code for Information Interchange, enables electronic communication. Thus, this standard is responsible for text representation in technology such as computers and telecommunications devices.
For example, if you want to convert a user resume to ASCII format, the finished product will be plain text without any text styling, such as tabs, bold, or underscores. Computers can quickly process this raw format. It also makes it simple for other apps to import and/or utilise the finished document.
The fact that we can carry out the majority of tasks with only commands is one of the best things about Linux. Character conversions are an excellent illustration of such procedures.
We'll go through a few methods for converting a hex character to ASCII from the command line in this brief lesson.
Use case
Let us take the following Hex Characters −
54 54 75 74 6F 72 69 61 6C 73 20 50 6F 69 6E 74 75 74 6F 72 69 61 6C 73 20 50 6F 69 6E 74
The comparable ASCII character representation of the aforementioned Hex characters is theoretically −
Tutorials Point
We will examine various Linux-based approaches to arrive at the aforementioned solution.
Method 1 : By Using printf Command
The printf command formats and prints data input in accordance with the data format specified.
We can use this command to convert a hex character to an ASCII character −
$ printf '\x54 \x75 \x74 \x6F \x72 \x69 \x61 \x6C \x73 \x20 \x50 \x6F \x69 \x6E \x74' && echo ''
$ printf '\x54 \x75 \x74 \x6F \x72 \x69 \x61 \x6C \x73 \x20 \x50 \x6F \x69 \x6E \x74' && echo '' Output: T u t o r i a l s P o i n t
In this example, we're simply representing a hex number with the \x format specifier.
Method 2 : By Using echo and xxd command
The xxd command is mostly used to create a hexdump of sampled Hex characters or to reverse the stated action. First, an echo command will accept the sample Hex characters as input before passing them to the xxd command, which will convert them to ASCII characters.
$ echo 54 75 74 6F 72 69 61 6C 73 20 50 6F 69 6E 74 | xxd -r -p && echo ''
$ echo 54 75 74 6F 72 69 61 6C 73 20 50 6F 69 6E 74 | xxd -r -p && echo '' Output: Tutorials Point
Here is the breakdown of the command
-r − It converts the Hex character into ASCII
-p − It will print the output in the plain text
echo − It will produce a new line on Linux terminal
Method 3 : By Using sed command
Before printing the final output, the sed command is used to filter and transform the Hex characters input by passing it through a regular expression for conversion to ASCII characters.
We used piping to collect the hash string via echo, convert it via sed to the given expression, and get the output as shown.
$ echo -n 5475746F7269616C7320506F696E74 | sed 's/\([0-9AF]\{2\}\)/\\\x\1/gI' | xargs printf && echo ''
$ echo -n 5475746F7269616C7320506F696E74 | sed 's/\([0-9AF]\{2\}\)/\\\x\1/gI' | xargs printf && echo '' Output: Tutorials Point
Method 4 : By Using dc command
Alternatively, we can use the dc command to convert a hex character to an ASCII character. Desk Calculator is abbreviated as dc. This command accepts input in the form of postfix notation.
In this case, 16i indicates that you are dealing with hex radix, and P prints the output of the dc command.
$ echo "16i 5475746F7269616C7320506F696E74 P" | dc
$ echo "16i 5475746F7269616C7320506F696E74 P" | dc Output: Tutorials Point
Because the converted output lacks the new line character, it is mixed with the prompt in the terminal, as shown above. You can add “echo” to the above command to make it more readable.
$ echo "16i 5475746F7269616C7320506F696E74 P" | dc && echo''
$ echo "16i 5475746F7269616C7320506F696E74 P" | dc && echo'' Output: Tutorials Point
Method 5 : By Using perl command
By using perl we can easily convert our hex data into ASCII format. Lets execute the following command on your Linux terminal.
$ echo 5475746F7269616C7320506F696E74 | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' && echo ''
$ echo 5475746F7269616C7320506F696E74 | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' && echo '' Output: Tutorials Point
Conclusion
In this article, we looked at several practical examples of converting a hex character to ASCII. We began with examples of the printf and echo commands. Then we executed the dc command. Finally, we saw the perl command in action. We can use these commands to increase our productivity in everyday life. These are the efficient methods to convert hex into ASCII characters in Linux shell.