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 binary bytes the string represents.
So if I do
echo 5a | command_im_looking_for > temp.txt
and I open temp.txt
, I will see a solitary letter Z.
dc can convert between numeric bases:
Note that this won't skip non-hex characters. If you want just the hex (no whitespace from the original string etc):
Also,
zsh
andbash
support this natively inecho
:I used to do this using xxd
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:
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.
Here is a pure bash script (as printf is a bash builtin) :
If bash is already running, this should be faster than any other solution which is launching a new process.
You can make it through echo only and without the other stuff. Don't forget to add "-n" or you will get a linebreak automatically: