Super User is a question and answer site for computer enthusiasts and power users. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

How to easily convert to/from plain machine-readable hexadecimal data (without any paddings/offsets/character view) with xdd or hexdump?

I'm tired of digging of some special format strings (and finding out that it suddenly starts wrapping lines after N characters or skip lines) or writing Perl one-liners every time.

Why is it not as simple as base64/base64 -d?

share|improve this question
Because almost no one needs it. – Ignacio Vazquez-Abrams May 5 '11 at 13:06
base64 does wrap the output by default. – grawity May 5 '11 at 13:09
up vote 6 down vote accepted
hexdump -e '"%x"'
xxd -p | tr -d '\n'

If you get tired of writing this every time, create an alias.

share|improve this answer
I'm tired of looking at man page and gettings confused either by necessity to compose hexdump's formt strings or by xxd's "postscript" thing. (Is this "postscript" something about printers or about letters?) – Vi. May 5 '11 at 13:59
@Vi: It could be related to the inclusion of binary data in PostScript documents. – grawity May 5 '11 at 14:03
@Vi, think of -p as "plain". I surmise the Postscript comment is because the Postscript language allows for in-line data (typically images) encoded exactly this way. So Postscript programmers may use xxd to convert binary images into a convenient form for embedding in a Postscript file. – RedGrittyBrick May 5 '11 at 14:05
$ xxd -p test.txt > test.hex
$ xxd -p -r test.hex test2.txt
$ diff test.txt test2.txt

Use alias or declare functions in your .profile to create mnemonics so you don't have to remember or dig about in man pages.

share|improve this answer
(was skipping "-p" option while reading man because of mysterious "postscript" thing) – Vi. May 5 '11 at 13:57
Wrapped output. – Vi. May 5 '11 at 13:59
@Vi: I understand. Out of curiosity, what is the problem with wrapped output (i.e. a small percent of arguably superfluous LF chars)? – RedGrittyBrick May 5 '11 at 14:01
It can be a problem if including hex in config files, like INIT=01303031313102 CMD1=41414141FF000000. – Vi. May 5 '11 at 15:02

Here is the version using od utility (part of coreutils package):

od -An < input | tr -dc '[:alnum:]'
share|improve this answer

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .