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.
Conversion hex string into ascii in bash command line - Stack Overflow
bash - How to convert hex to ASCII characters in the Linux shell? - Stack Overflow
convert hex to ascii text
Ascii/Hex convert in bash - Stack Overflow
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.
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
I used to do this with 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.
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'
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 all, freshly joined noobie here :)
I am currently working as a jr embedded software engineer, and have been struggling with data collection at runtime of the application.
I'm using a debugger that keeps sending a variable's hex value to the host pc via usb, but since this value is interpreted as ASCII, I see invalid symbols on the terminal.
As naive as it may sound, my question is: is there a way with a script to "get in between" the debugger and the terminal on the host pc to convert these hex values in their ASCII counterpart, so they are displayable "correctly"? (like, if I send 0x0123 I'd like the terminal to show "291" instead of the symbols associated with 0x01 and 0x23).
Extra question: do you have any suggestion on material I can study on to get a solid knowledge of bash scripting in general, too?
Thank you for your time and your patience, I hope I didn't sound too stupid haha.
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!
Sorry if I'm a bit off with my terminology.
I have a binary file, a ROM for a gameboy game. Let's assume that between the offsets of AC000 and AF000 is a bunch of text encoded in hex.
Let's say I have a basic text file with hex data (or binary data) written in ascii. That is, the actual text of the file is "48 65 6c 6c 6f 20 57 6f 72 6c 64 21"(formatted in whatever the best way to format it to solve my problem would be), but the actual hex of the file is "34 38 20 36 35 20 36 63 20 36 63 20 36 66 20 32 30 20 35 37 20 36 66 20 37 32 20 36 63 20 36 34 20 32 31" How would I make an actual hex file with the proper hex data?
That's the first part of my question. The second part is how do you insert that data into another hex file at a certain offset over writing the data already there? xiv, which is a windows text editor, has the functionality of being able to import another file, but it does not overwrite. Which is okay I guess but inconvenient because I have to delete all the data it moves. Additionally, I'd like to be able to do this without using wine.
Because I feel like I am being confusing, here is a picture of what I want to do: http://i.imgur.com/pCyOLEx.png
Thank you greatly for any help.
EDIT: I can't edit the data manually because it is a LOT of hex data. Instead, I am converting text to the proper hex values, and I want to IMPORT the file directly, because that is much much quicker than typing in the individual values for each letter.
EDIT: After a lot of hair-pulling, I think I've solved this.
To fix the first problem (the turning ascii hex into actual hex), I did
xxd -p -r ascii.file patch.file
The -p is VERY important.
Then, once I have patch.file, I do
dd if=patch.file of=source.file seek=56000 obs=1 conv=notrunc count=2000 bs=1
Where "seek" is the offset I want the imported data to start, bs is set to 1 to indicate I want it to count by one byte, and count is how many bytes are in the patch.file
Thanks to everyone who helped.