Doing a Reverse Hex Dump


If you work with the command line you've most likely used hexdump or od to dump binary files, but what do you do if you have a hex dump of something and you want to create the binary version of the data? Assuming your needs aren't too complex, the answer may be xxd. You can use xxd to dump binary files just like hexdump and od, but you can also use it to do the reverse: turn a hex dump back into binary.

If you run xxd with just a file name it dumps the data in a fairly standard hex dump format:

  # xxd bdata
  0000000: 0001 0203 0405                           ......
Now if you pipe the output back to xxd with the -r option and redirect that to a new file, you can convert the hex dump back to binary:
  # xxd bdata | xxd -r >bdata2
  # cmp bdata bdata2
  # xxd bdata2
  0000000: 0001 0203 0405                           ......
Note that when doing reverse conversions with xxd, the data needs to look like a hex dump: there needs to be an offset and the data needs to be formatted correctly. So, for example, this works:
  # echo 01: 01 02 03 04 | xxd -r >output
but this does not because the data is not formatted correctly:
  # echo 01: 1 2 3 4 | xxd -r >output

As a more concrete example, I recently had a need to create a Motorola S-Record file containing a MAC Address. First thing I needed was a way to create a binary file with the MAC Address so that I could use objcopy to convert it to an S-Record file. A bit of pondering produced no good ideas. Linux and its brethren have a lot of command line utilities for manipulating text but not many for manipulating binary data. Of course, I could have written a C program to create the binary file or to create the S-Record file itself, but that seemed a bit much considering I was only talking about 6 bytes of data.

After a fair bit of searching the net I came across xxd. In OpenSuSE xxd is part of the vim-base package. I'm not sure if that's where it is in all distros, since it doesn't seem to have any relationship to vim.

The script takes a MAC Address and outputs S-Record data:

  # sh 00:11:22:33:44:55
Optionally, you can specify an address for re-basing the S-Record file and an output file name:
  # sh --address 0xffff0000 --output ma 00:11:22:33:44:55
  # cat ma
See the entire script is here


Mitch Frazier is an Associate Editor for Linux Journal.


Comment viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.


Alexander Janssen's picture

Mitch, I'm deeply impressed. I'm just wondering how I could have live without that tool? I remember several cases when I needed to do exactly that thing and I always ended up writing a program of some sort which was dumped afterwards...

Very cool!

White Paper
Linux Management with Red Hat Satellite: Measuring Business Impact and ROI

Linux has become a key foundation for supporting today's rapidly growing IT environments. Linux is being used to deploy business applications and databases, trading on its reputation as a low-cost operating environment. For many IT organizations, Linux is a mainstay for deploying Web servers and has evolved from handling basic file, print, and utility workloads to running mission-critical applications and databases, physically, virtually, and in the cloud. As Linux grows in importance in terms of value to the business, managing Linux environments to high standards of service quality — availability, security, and performance — becomes an essential requirement for business success.

Learn More

Sponsored by Red Hat

White Paper
Private PaaS for the Agile Enterprise

If you already use virtualized infrastructure, you are well on your way to leveraging the power of the cloud. Virtualization offers the promise of limitless resources, but how do you manage that scalability when your DevOps team doesn’t scale? In today’s hypercompetitive markets, fast results can make a difference between leading the pack vs. obsolescence. Organizations need more benefits from cloud computing than just raw resources. They need agility, flexibility, convenience, ROI, and control.

Stackato private Platform-as-a-Service technology from ActiveState extends your private cloud infrastructure by creating a private PaaS to provide on-demand availability, flexibility, control, and ultimately, faster time-to-market for your enterprise.

Learn More

Sponsored by ActiveState