Linux – hexdump vs xxd format difference

conversionhexdumplinuxxxd

I was searching for how to do a reverse hexdump and found xxd mentioned. However, it does not seem to work with simply:

xxd -r hexdumpfile > binaryfile

I then compared the difference between outputs of xxd infile and hexdump infile, and found three differences:

  1. xxd output has a colon after the address
  2. xxd output has the positions in the data reversed (for example, 5a42 in hexdump output becomes 425a in xxd output)
  3. There are some extra characters after each line

I only have the hexdumped version of certain files on a server. How can I correctly get back the binary data using xxd?

Best Answer

There's no one command that I know of that will do the conversion, but it can easily be broken up into a few steps:

  1. Strip addresses from hexdump output using sed
  2. Convert into binary using xxd
  3. Endian conversion (for example, 5a42 becomes 425a) using dd

Here's the full command:

sed 's/^[0-9]*//' hexdump | xxd -r -p | dd conv=swab of=binaryfile
Related Question