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

babakks

Babak K. Shandiz

Posted on November 14, 2021

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

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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

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!"
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Which would result in:

Hello World!
Enter fullscreen mode Exit fullscreen mode

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

xxd --help
Enter fullscreen mode Exit fullscreen mode

or

man xxd
Enter fullscreen mode Exit fullscreen mode

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.

💖 💪 🙅 🚩
babakks
Babak K. Shandiz

Posted on November 14, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related