linux shell scripting: hex string to bytes

2019-01-13 04:24发布

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.

12条回答
Summer. ? 凉城
2楼-- · 2019-01-13 04:32

dc can convert between numeric bases:

$ echo 5a | (echo 16i; tr 'a-z' 'A-Z'; echo P) | dc
Z$
查看更多
一夜七次
3楼-- · 2019-01-13 04:35
echo -n 5a | perl -pe 's/([0-9a-f]{2})/chr hex $1/gie'

Note that this won't skip non-hex characters. If you want just the hex (no whitespace from the original string etc):

echo 5a | perl -ne 's/([0-9a-f]{2})/print chr hex $1/gie'

Also, zsh and bash support this natively in echo:

echo -e '\x5a'
查看更多
Viruses.
4楼-- · 2019-01-13 04:40

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.

查看更多
来,给爷笑一个
5楼-- · 2019-01-13 04:40

Here is a pure bash script (as printf is a bash builtin) :

#warning : spaces do matter
die(){ echo "$@" >&2;exit 1;}

p=48656c6c6f0a

test $((${#p} & 1)) == 0 || die "length is odd"
p2=''; for ((i=0; i<${#p}; i+=2));do p2=$p2\\x${p:$i:2};done
printf "$p2"

If bash is already running, this should be faster than any other solution which is launching a new process.

查看更多
Lonely孤独者°
6楼-- · 2019-01-13 04:41

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:

echo -n -e "\x5a"
查看更多
时光不老,我们不散
7楼-- · 2019-01-13 04:42
echo 5a | python -c "import sys; print chr(int(sys.stdin.read(),base=16))"
查看更多
登录 后发表回答