How to convert file data to plain hex?

hexdump -ve '1/1 "%02x"'
xxd -p | tr -d '\n'

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


How to easily convert to/from plain machine-readable hexadecimal data

In brief.

$ xxd -plain test.txt > test.hex
$ xxd -plain -revert test.hex test2.txt
$ diff test.txt test2.txt
$

Explanation:

$ xxd -plain test.txt > test.hex

This writes a hex encoding of the data in test.txt into new file test.hex. The -p or -plain option makes xxd use "plain" hex format with no spaces between pairs of hex digits (i.e. no spaces between byte values). This converts "abc ABC" to "61626320414243". Without the -p it would convert the text to a 16-bit word oriented traditional hexdump format, which is arguably easier to read but less compact and therefore less suitable as a transmission format and slightly harder to reverse.

$ xxd -plain -revert text.hex test2.txt

This uses the -r or -revert option for reverse operation. The -plain option is used again to indicate that the input hex file is in plain format.

I make the output filename different from the original filename so we can later compare the results with the original file.

$ diff test.txt test2.txt
$ 

The diff command outputs nothing - this means there is no difference between the original and reconstituted file contents.


I'm tired of digging of some special format strings

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

or just remember -plain and -revert.


Wrapped output

Yes, there are new-line characters in the output. You want to avoid that. You can use the -c or -cols option to specify how long you want the output lines to be to attempt to avoid line-wrapping of the output. -c 0 gives the default length and the man page suggests 256 is the limit but it seems to work beyond that.

$ xxd -plain -cols 9999 test.txt > test.hex
$ wc test.txt test.hex
  121   880  4603 test.txt
    1     1  9207 test.hex

The wc wordcount command tells us how many lines, words and characters are in each file.

So 121 lines (880 words, 4603 bytes) of ASCII text were encoded as 1 line of hex digits.


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

od -An < input | tr -dc '[:alnum:]'