DEV Community

Babak K. Shandiz
Babak K. Shandiz

Posted on • Originally published at babakks.github.io on

2 1

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.

Image of Datadog

Master Mobile Monitoring for iOS Apps

Monitor your app’s health with real-time insights into crash-free rates, start times, and more. Optimize performance and prevent user churn by addressing critical issues like app hangs, and ANRs. Learn how to keep your iOS app running smoothly across all devices by downloading this eBook.

Get The eBook

Top comments (0)

The Most Contextual AI Development Assistant

Pieces.app image

Our centralized storage agent works on-device, unifying various developer tools to proactively capture and enrich useful materials, streamline collaboration, and solve complex problems through a contextual understanding of your unique workflow.

👥 Ideal for solo developers, teams, and cross-company projects

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay