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

Neat right?

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:

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

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

Leave a Reply

Your email address will not be published. Required fields are marked *