ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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 ...
Where there are three fields you can access in awk. $1 is the offset (in hex), $2 is the two-digit hex representation of a byte, and $3 is the ASCII representation of the byte (or just a period for nonprintable characters).
For going back, just do something like:
Code:
... | xxd -p -s -
The only formatting restrictions on the input is that single bytes appear together as two-digit numbers. It doesn't care about formatting or whitespace (the -p is for plain).
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).
Distribution: Arch Linux && OpenBSD 7.4 && Pop!_OS && Kali && Qubes-Os
Posts: 824
Original Poster
Rep:
Quote:
Originally Posted by osor
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).
Thats true, but I'm not that concerned about portability, (I'm using slax based Back|Track and adding modules is easy) and I'm not doing anything really important, just learning to write my own snort rules and I was getting tired to visit online hex translators
But anyways, that ascii2hex is working nicely with my script so thanks again.
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.
I used a '(..)' subshell in order to keep the IFS setting local. It's needed in order to print the '*' expansion without spaces between each entry. Changing '*' to '@' would print every character on a separate line.
Last edited by David the H.; 05-15-2013 at 02:36 PM.
Reason: as stated
"try.sh" 25 lines, 893 characters
\x41\x20\x42\x20\x43
awk: illegal field $(), name "_"
input record number 1, file
source line number 1
Code:
uname -a
FreeBSD y-es.ru 10.0-RELEASE FreeBSD 10.0-RELEASE #0 r260789: Thu Jan 16 22:34:59 UTC 2014 root@snap.freebsd.org:/usr/obj/usr/src/sys/GENERIC amd64
Works fine for me with both bash and sh. It looks to me like you have an outdated version of awk that doesn't support "$_", but I have no experience with FreeBSD. I'm running GNU awk 3.1.8
Last edited by suicidaleggroll; 03-20-2014 at 12:44 PM.
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.