This worked for me.
$ echo -n 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$
echo -n prevents a newline from being added to the end
-r tells it to convert hex to ascii as opposed to its normal mode of doing the opposite
-p tells it to use a plain format.
This worked for me.
$ echo -n 54657374696e672031203220330 | xxd -r -p
Testing 1 2 3$
echo -n prevents a newline from being added to the end
-r tells it to convert hex to ascii as opposed to its normal mode of doing the opposite
-p tells it to use a plain format.
This code will convert the text 0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2 into a stream of 11 bytes with equivalent values. These bytes will be written to standard out.
TESTDATA=$(echo '0xA7.0x9B.0x46.0x8D.0x1E.0x52.0xA7.0x9B.0x7B.0x31.0xD2' | tr '.' ' ')
for c in $TESTDATA; do
echo $c | xxd -r
done
As others have pointed out, this will not result in a printable ASCII string for the simple reason that the specified bytes are not ASCII. You need post more information about how you obtained this string for us to help you with that.
How it works: xxd -r translates hexadecimal data to binary (like a reverse hexdump). xxd requires that each line start off with the index number of the first character on the line (run hexdump on something and see how each line starts off with an index number). In our case we want that number to always be zero, since each execution only has one line. As luck would have it, our data already has zeros before every character as part of the 0x notation. The lower case x is ignored by xxd, so all we have to do is pipe each 0xhh character to xxd and let it do the work.
The tr translates periods to spaces so that for will split it up correctly.
bash - How to convert hex to ASCII while preserving non-printable characters - Stack Overflow
Ascii/Hex convert in bash - Stack Overflow
Convert column from hex to ascii
How do you "convert" ascii "hex" data into actual hex, and how can you insert it into a binary file at a specific offset?
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.
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
Hi! I need help with something, I have a tab separated file with one column (number 6) with values in hex, I need to convert them to ascii text.
I can modify the column using awk but I can't pass that value to another command to do the convertion. Is it possible to do something like this?
Thanks!
Your input file looks like it was produced using something like this:
hexdump -e '"%08_ad " 8/1 "%04x "' -e '"" 0/0 "" "\n"' original_file
Unfortunately, xxd -r can't deal with decimal offsets.
Here is a short Gnu AWK program to give you the output you're looking for:
gawk '{printf "%s |", $0; for (f=2; f<=9; f++) { c = strtonum("0x" $f); if (c >= 32 && c <= 126) printf "%c",c; else printf "."}; printf "|\n"}' input_file
If you're using an AWK other than gawk, you can use the strtonum() function here.
Here's another way to do the same thing as the gawk script above:
cut -c 11- input_file | sed 's/\<00//g' | xxd -r -p | hexdump -e '"%08_ad " 8/1 "%04x " ""' -e '" |" 8/1 "%_p" "|\n"'
If, instead, you want to convert your input file to text:
cut -c 11- input_file | xxd -r -p
iconv -f utf-16be -t ascii input.txt