Sometimes you may need to investigate the contents of binary files. Simply using cat to view these details in your terminal can have all sorts of random effects due to control characters, etc. The utility hexdump allows you to look at the contents of these files in a sane way.
From the hexdump manpage:
display file contents in hexadecimal, decimal, octal, or ascii
In today’s article, we’ll walk through some example usages of this utiltiy.
Examples
For all of these examples, we’ll be using a 256 byte file of random binary. I generated this data on my system with the following command:
The initial view of this data now looks like this:
Formatting
Now things get interesting. The -e switch of the hexdump command allows us to specify a format string that controls the output to the terminal.
Using _a[dox] we can control how that offset down the left hand side looks. %07_ax pads the offset with a width of 7. 16/1 "%_p" will print 16 bytes using _p which prints using the default character set. The output of which looks like this:
Anytime this format encounters a non-printable character, a . is put in its place.
Builtin
-v -C gives a side-by-side of hex values along with the printable characters: