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
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With