Babak K. Shandiz
Babak K. Shandiz's Blog

Babak K. Shandiz's Blog

Binary/Hex Conversion in Linux Shell with xxd [RE#13]

Babak K. Shandiz's photo
Babak K. Shandiz
·Nov 14, 2021·

2 min read

It’s not that unusual to need to see/check/verify the binary output of a shell command in HEX format. Clearly, I mean without picking up a HEX editor or something like that. Last time, it happened to me a few days ago when I was to inspect the content of a base64-encoded string. The tool to the rescue is (of course not limited to) xxd. 🏅

Using xxd, to get the HEX representation for some binary content you just need to call it with no argument:

echo -n 'Computer says: "Hello World!"' | xxd

Depending on the case it could be more insightful to also have the ASCII values column. That’s simply done by calling xxd with -C option:

echo -n 'Computer says: "Hello World!"' | xxd -C

Which results in such a neat output:

00000000: 436f 6d70 7574 6572 2073 6179 733a 2022  Computer says: "
00000010: 4865 6c6c 6f20 576f 726c 6421 22         Hello World!"

Also, xxd can do things the other way. For instance, may you need to turn a HEX string into a binary content, just call xxd with -r option, like this:

echo -n "0x48656c6c6f20576f726c6421" | xxd -r

Which would result in:

Hello World!

There are more xxd options which you can find by checking:

xxd --help

or

man xxd

About Regular Encounters
I’ve decided to record my daily encounters with professional issues on a somewhat regular basis. Not all of them are equally important/unique/intricate, but are indeed practical, real, and of course, textually minimal.

 
Share this