Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ascii/Hex convert in bash

Tags:

bash

hex

ascii

I'm now doing it this way:

[root@~]# echo Aa|hexdump -v 0000000 6141 000a                               0000003 [root@~]# echo -e "\x41\x41\x41\x41" AAAA 

But it's not exactly behaving as I wanted,

the hex form of Aa should be 4161,but the output is 6141 000a,which seems not making sense.

and when performing hex to ascii,is there another utility so that I don't need the prefix \x ?

like image 686
gdb Avatar asked Apr 20 '11 02:04

gdb


1 Answers

The reason is because hexdump by default prints out 16-bit integers, not bytes. If your system has them, hd (or hexdump -C) or xxd will provide less surprising outputs - if not, od -t x1 is a POSIX-standard way to get byte-by-byte hex output. You can use od -t x1c to show both the byte hex values and the corresponding letters.

If you have xxd (which ships with vim), you can use xxd -r to convert back from hex (from the same format xxd produces). If you just have plain hex (just the '4161', which is produced by xxd -p) you can use xxd -r -p to convert back.

like image 146
Random832 Avatar answered Sep 22 '22 19:09

Random832