[bash] ASCII to HEX and hex to ascii
What kind of command should I use to translate ascii to hex in a bash script ? (and hex back to ascii)
TIA ///// |
do you mean something like this?
Code:
echo 0x41 | awk '{printf "%c\n", $1}' http://devworld.apple.com/documentat...section_3.html And here's yet another possibility: Code:
echo A|hexdump Code:
echo A|od -x |
Hi, yes something like that. Only but is that..
Code:
echo 0x41 | awk '{printf "%c\n", $1}' awk -W version GNU Awk 3.1.5 Code:
awk '{printf "%c\n", $1}' /root/Desktop/testfile # testfile contains 0x41 Cheers //// Edit: Thank you, I can use that hexdump, and that link is a nice one :) |
Quote:
Code:
ada@barnabas:~> echo -e "\x41" | awk '{printf "%c\n", $1}' |
alternative in Python:
ascii to hex: Code:
#!/usr/bin/python '0x61' hex to ascii Code:
import binascii 'a' |
... and I just learned that there's a package named uni2ascii which does the following:
Code:
ada@barnabas:~> echo "ABCDE" | uni2ascii -Bsepq |
Quote:
|
There's a nice utility that comes with Vim called xxd. It is script-friendly, and (unlike od or hexdump) has the ability to go back and forth (with the revert option).
For example, you could have something like: Code:
... | xxd -g 1 -c 1 | awk ... For going back, just do something like: Code:
... | xxd -p -s - I guess xxd's not as portable as hexdump or od, but it is probably more portable than uni2ascii (i.e., is more likely to be found installed on a *nix machine than uni2ascii). |
Quote:
But anyways, that ascii2hex is working nicely with my script so thanks again. Code:
slax ~ # /root/Desktop/trans 'Hello there !' |
Another awk alternative with more than one character
Code:
echo -e "\x41\x20\x42\x20\x43" | awk '{printf "%s\n", $_}' Quote:
|
What if your string is:
48656C6C6F2074686572652021 And you don't have perl, python, xxd or uni2ascii? And you are not allowed to install it. How can you convert it? Or course I could always write a loop and read it in pairs: Code:
#!/bin/bash |
Hex to ascii is dead easy; bash can do it internally, if they're properly formatted. Just use "echo -e", printf's %b format token, or the $'..' quoting form. See the sections on QUOTING and the echo built-in in the bash man page for full details on what these options can expand.
So in post #10 above, the awk command is actually completely superfluous. The characters are converted by echo before they even reach it. As for the reverse direction, there's no built-in way to do it, so see the above. I've also used uniname before to get the hex codes of a string of characters. Edit: Example use for the string in #9: Code:
$ hexchars=( 48 65 6C 6C 6F 20 74 68 65 72 65 20 21 ) |
You guys with awk are silly... your awk is not doing anything at all. Sorry to point that out. :P
Code:
$ echo -e "\x41\x20\x42\x20\x43" |cat |
Code:
#!/bin/sh Code:
"try.sh" 25 lines, 893 characters Code:
uname -a |
Quote:
|
awk -version
awk version 20121220 (FreeBSD) strange. System is updated. So its a third side soft |
SPF - Thanks!
Quote:
Thanks, Matt |
I was trying to easily convert back and forth between C-syntax Hex and Ascii (used a lot in Arduino, because apparently that's a fun way to store web pages on embedded controllers), and stumbled upon this thread. It was useful to completing my task, so thought it worth giving back. If nothing else, when I need to do this for something else and misplace these little utils, perhaps my web search will bring me here. :p
The following scriptlettes should be rather light weight and only use fairly standard POSIX utils (od, sed, bash). $ cat asc2hex Code:
#!/bin/bash Code:
#!/bin/bash Code:
$ echo "Hello World!"|./asc2hex Code:
$ cat hello_world |
All times are GMT -5. The time now is 05:15 PM. |