Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert hex to ASCII characters in the Linux shell?

Lets say that I have a string 5a.

This is the hex representation of the ASCII letter Z.

I need to know a Linux shell command which will take a hex string and output the ASCII characters that the string represents.

So if I do:

echo 5a | command_im_looking_for 

I will see a solitary letter Z:

Z 
like image 550
Krystian Cybulski Avatar asked Oct 22 '09 02:10

Krystian Cybulski


People also ask

Does Linux use ASCII?

ASCII — Most widely used for English before 2000. UTF-8 — Used in Linux by default along with much of the internet. UTF-16 — Used by Microsoft Windows, Mac OS X file systems and others.

What does xxd command do in Linux?

DESCRIPTION. xxd creates a hex dump of a given file or standard input. It can also convert a hex dump back to its original binary form. Like uuencode(1) and uudecode(1) it allows the transmission of binary data in a 'mail-safe' ASCII representation, but has the advantage of decoding to standard output.

How can I see the ASCII table in Linux?

Method 1: From the TerminalAt the Bash prompt, type the command man ASCII and then push enter. You'll be greeted with the technical definition of what ASCII is, followed by a table of the 128 characters that make up the proper ASCII standard.


1 Answers

I used to do this using xxd

echo -n 5a | xxd -r -p 

But then I realised that in Debian/Ubuntu, xxd is part of vim-common and hence might not be present in a minimal system. To also avoid perl (imho also not part of a minimal system) I ended up using sed, xargs and printf like this:

echo -n 5a | sed 's/\([0-9A-F]\{2\}\)/\\\\\\x\1/gI' | xargs printf 

Mostly I only want to convert a few bytes and it's okay for such tasks. The advantage of this solution over the one of ghostdog74 is, that this can convert hex strings of arbitrary lengths automatically. xargs is used because printf doesnt read from standard input.

like image 161
josch Avatar answered Oct 14 '22 15:10

josch