Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Convert Hex to ASCII Characters in the Linux Shell
Hexadecimal (Hex) is a base-16 numbering system that uses digits 0-9 and letters A-F to represent values. Converting hex to ASCII characters is a common task in Linux systems, especially when dealing with encoded data, network protocols, or binary file analysis.
Hex digits: 0 1 2 3 4 5 6 7 8 9 A B C D E F
ASCII (American Standard Code for Information Interchange) is a character encoding standard that represents text in computers and communication equipment. Each ASCII character corresponds to a specific numeric value that can be expressed in hexadecimal format.
Use Case Example
Let's convert the following hex string to ASCII characters:
5475746F7269616C7320506F696E74
This hex string represents the ASCII text Tutorials Point. We'll explore five different methods to perform this conversion in Linux.
Method 1: Using printf Command
The printf command formats and prints data according to specified format specifiers. Use \x prefix to indicate hexadecimal values:
$ printf '\x54\x75\x74\x6F\x72\x69\x61\x6C\x73\x20\x50\x6F\x69\x6E\x74' && echo ''
Tutorials Point
Method 2: Using echo and xxd Commands
The xxd command creates hex dumps or reverses them back to original format. Combined with echo, it provides an efficient conversion method:
$ echo 5475746F7269616C7320506F696E74 | xxd -r -p && echo ''
Tutorials Point
Command options:
-rReverses hex dump back to original format-pUses plain hex dump style without line numbers and ASCII columns
Method 3: Using sed Command
The sed command transforms the hex string using regular expressions, converting pairs of hex digits into escape sequences:
$ echo -n 5475746F7269616C7320506F696E74 | sed 's/\([0-9AF]\{2\}\)/\\\x\1/gI' | xargs printf && echo ''
Tutorials Point
Method 4: Using dc Command
The dc (desk calculator) command processes input in postfix notation. It can convert hex numbers directly to ASCII:
$ echo "16i 5475746F7269616C7320506F696E74 P" | dc && echo ''
Tutorials Point
Command components:
16iSets input radix to hexadecimal (base 16)PPrints the top stack value as ASCII characters
Method 5: Using perl Command
Perl provides powerful pattern matching capabilities for hex-to-ASCII conversion using regular expressions:
$ echo 5475746F7269616C7320506F696E74 | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie' && echo ''
Tutorials Point
Comparison of Methods
| Method | Best For | Complexity | Availability |
|---|---|---|---|
| printf | Simple, direct conversion | Low | Built-in |
| xxd | Large hex dumps | Low | Usually installed |
| sed | Pattern transformation | High | Built-in |
| dc | Mathematical operations | Medium | Usually installed |
| perl | Complex text processing | Medium | Often installed |
Conclusion
Converting hex to ASCII in Linux can be accomplished through multiple command-line tools, each with distinct advantages. The printf and xxd methods are most straightforward for simple conversions, while perl and sed offer more flexibility for complex pattern processing. Choose the method that best fits your specific use case and system environment.
