Ascii/Hex convert in bash
Solution 1
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.
Solution 2
For the first part, try
echo Aa | od -t x1
It prints byte-by-byte
$ echo Aa | od -t x1
0000000 41 61 0a
0000003
The 0a
is the implicit newline that echo produces.
Use echo -n
or printf
instead.
$ printf Aa | od -t x1
0000000 41 61
0000002
Solution 3
$> printf "%x%x\n" "'A" "'a"
4161
Solution 4
For single line solution:
echo "Hello World" | xxd -ps -c 200 | tr -d '\n'
It will print:
48656c6c6f20576f726c640a
or for files:
cat /path/to/file | xxd -ps -c 200 | tr -d '\n'
For reverse operation:
echo '48656c6c6f20576f726c640a' | xxd -ps -r
It will print:
Hello World
Solution 5
With bash :
a=abcdefghij
for ((i=0;i<${#a};i++));do printf %02X \'${a:$i:1};done
6162636465666768696A
gdb
Updated on July 18, 2022Comments
-
gdb almost 2 years
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 be4161
,but the output is6141 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
? -
gdb about 13 yearsHow to convert hex back to ascii?
-
gdb about 13 years
xxd -p -r AAA
,this doesn't work,can it retrieve parameters from command line directly? -
Random832 about 13 yearsno... and the 'r' converts back from hex, you'd want something like '
echo 4161 | xxd -r -p
' or 'echo Aa | xxd -p
' -
SourceSeeker almost 12 years@gdb: See printf where it says: "If the leading character is a single-quote or double-quote, the value shall be the numeric value in the underlying codeset of the character following the single-quote or double-quote."
-
Yokai over 6 years
python
!=bash
-
jcomeau_ictx over 6 yearsneither are
sed
,hexdump
,printf
,awk
, orxxd
. -
jcomeau_ictx over 6 yearsit's shorter than some of the other solutions and arguably more readable. and it converts both ways, from the Bash command line. I don't see your point.
-
Giac over 6 yearswrong output for some characters like (space, tab, \r,\n) all of these characters will be shown as \x00
-
Yokai over 6 yearsThe point is, he asked for a bash solution. Meaning, as per his context, bashisms or bash built-ins. You didn't provide this with python solution(s).
-
Yokai over 6 yearsThe c-style
printf
solution here should be the accepted answer. It is the most portable and the most simple, and keeps with POSIX compliance without needing external tools that may or may not be available. -
Casey Flynn over 4 years
echo -n "Hello, world!" | xxd -ps -c 200
would also work -
ssc almost 4 yearsI typically use
xxd -ps -c 200 <<< "Hello, world!"
-
cary over 3 yearsexample: echo -e "\x68"