Linux hex to ascii

Converting Hex to ASCII Using xxd

Strings are commonly encoded as hexadecimal (hex) strings for various purposes. Hence, it is also common to convert hex strings to its original strings such as an ASCII string. Hex to ASCII string conversion can be done programmatically in many languages. In command line, we can use xxd to convert hex to ASCII string. This makes hex to ascii string conversion easy and handy, especially during debugging.

xxd and xxd functions

Let’s first take a look at xxd , its functions and command line options.

Introduction to xxd

xxd is a tool to make a hexdump or do the reverse. That is, xxd can create a hex dump of a given file or standard input, and also convert a hex dump back to its original binary form. For ASCII strings, the “binary form” is the plain string form.

For ASCII to Hex, xxd generate the hex dump as follows with its default options.

$ echo "abcdefghijklmnopqrstuvwxyz" | xxd 00000000: 6162 6364 6566 6768 696a 6b6c 6d6e 6f70 abcdefghijklmnop 00000010: 7172 7374 7576 7778 797a 0a qrstuvwxyz.

Here, the hex string for “abcdefghijklmnopqrstuvwxyz” is “6162636465666768696a6b6c6d6e6f707172737475767778797a0a”.

xxd options

Although by default xxd uses its own 3-column format for the hex dump, xxd supports several options for different functions and formats. Following are some options we will use for the purpose of hex to ASCII string conversion.

-p | -ps | -postscript | -plain

Output in postscript continuous hexdump style. Also known as plain hexdump style.

-r | -revert

Reverse operation: convert (or patch) hexdump into binary. If not writing to stdout, xxd writes into its output file without truncating it. Use the combination -r -p to read plain hexadecimal dumps without line number information and without a particular column layout. Additional Whitespace and line-breaks are allowed anywhere.

-u

Use upper case hex letters. Default is lower case.

Convert hex to ASCII using xxd

Now let’s see how to convert the hex string to ASCII string with the above example for the given hex string “6162636465666768696a6b6c6d6e6f707172737475767778797a0a”.

Convert lower case hex to ASCII using xxd

We combine the options -p and -r together for xxd and we can get the ASCII string.

$ echo "6162636465666768696a6b6c6d6e6f707172737475767778797a0a" | xxd -p -r abcdefghijklmnopqrstuvwxyz

Convert upper case hex to ASCII using xxd

If the hex string is in upper case, add the -u option.

$ echo "6162636465666768696A6B6C6D6E6F707172737475767778797A0A" | xxd -p -r abcdefghijklmnopqrstuvwxyz

2 comments:

how do I convert this data to string dumps
“871EA455E4F2618CBFBB6A607ED88F4B848265D4BB357B0275784ABB2A1584084DBE8C71753FFCBDCADBD9321A2E55422C58B8C6CA398D013D01716B87B0050BF1DD52AF8D657F769621457D8EEFE262FCD32E6F23C192767BEA6DF33841F9BE25F4C9427DA3B7B4F2E937B196425A5F197C25F7D0C545FBFCD32E6F23C192767BEA6DF33841F9BE290CB5D22715F507

Читайте также:  Альтернатива visio для linux

frankzhou@XA014D:~/github/gsm-pdu-decoder$ echo -n 871EA455E4F2618CBFBB6A607ED88F4B848265D4BB357B0275784ABB2A1584084DBE8C71753FFCBDCADBD9321A2E55422C58B8C6CA398D013D01716B87B0050BF1DD52AF8D657F769621457D8EEFE262FCD32E6F23C192767BEA6DF33841F9BE25F4C9427DA3B7B4F2E937B196425A5F197C25F7D0C545FBFCD32E6F23C192767BEA6DF33841F9BE290CB5D22715F507 | xxd -r -pi | xxd -i
0x87, 0x1e, 0xa4, 0x55, 0xe4, 0xf2, 0x61, 0x8c, 0xbf, 0xbb, 0x6a, 0x60,
0x7e, 0xd8, 0x8f, 0x4b, 0x84, 0x82, 0x65, 0xd4, 0xbb, 0x35, 0x7b, 0x02,
0x75, 0x78, 0x4a, 0xbb, 0x2a, 0x15, 0x84, 0x08, 0x4d, 0xbe, 0x8c, 0x71,
0x75, 0x3f, 0xfc, 0xbd, 0xca, 0xdb, 0xd9, 0x32, 0x1a, 0x2e, 0x55, 0x42,
0x2c, 0x58, 0xb8, 0xc6, 0xca, 0x39, 0x8d, 0x01, 0x3d, 0x01, 0x71, 0x6b,
0x87, 0xb0, 0x05, 0x0b, 0xf1, 0xdd, 0x52, 0xaf, 0x8d, 0x65, 0x7f, 0x76,
0x96, 0x21, 0x45, 0x7d, 0x8e, 0xef, 0xe2, 0x62, 0xfc, 0xd3, 0x2e, 0x6f,
0x23, 0xc1, 0x92, 0x76, 0x7b, 0xea, 0x6d, 0xf3, 0x38, 0x41, 0xf9, 0xbe,
0x25, 0xf4, 0xc9, 0x42, 0x7d, 0xa3, 0xb7, 0xb4, 0xf2, 0xe9, 0x37, 0xb1,
0x96, 0x42, 0x5a, 0x5f, 0x19, 0x7c, 0x25, 0xf7, 0xd0, 0xc5, 0x45, 0xfb,
0xfc, 0xd3, 0x2e, 0x6f, 0x23, 0xc1, 0x92, 0x76, 0x7b, 0xea, 0x6d, 0xf3,
0x38, 0x41, 0xf9, 0xbe, 0x29, 0x0c, 0xb5, 0xd2, 0x27, 0x15, 0xf5, 0x07

Источник

Convert Hex to ASCII Characters in the Linux Shell

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 −

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.

Читайте также:  Owasp zap kali linux install

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]\\)/\\\x\1/gI' | xargs printf && echo ''
$ echo -n 5475746F7269616C7320506F696E74 | sed 's/\([0-9AF]\\)/\\\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])/print chr hex $1/gie' && echo ''
$ echo 5475746F7269616C7320506F696E74 | perl -ne 's/([0-9a-f])/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.

Читайте также:  Linux get path to bin

Источник

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:

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

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

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]\\)/\\\\\\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.

Источник

Оцените статью
Adblock
detector