xxd
is a Linux tool that allows you to view binary files.
Why would you want to view binary files?
- To compare the compiled program with what you intended
- To inspect the contents of image or video files
- Out of pure curiosity
Usage of xxd
xxd filename
This command will output the content of the file like this:
00000000: 4500 6d00 7000 7400 7900 0000 0000 0000 E.m.p.t.y.......
00000010: 0000 0000 0000 0000 0000 0000 0000 0000 ................
It can be divided into three sections: the first one is the hex dump address, each line represents 16 characters (by default), the second section is the file content (in ASCII encoding), and the rightmost section displays the original values of the file. If there are any characters that cannot be displayed, a .
will be shown.
xxd -r filename
This command can be used to restore the binary file to its original form.
Some combinations of techniques
xxd filename | vim -
: View a binary file usingvim
- Open
vim filename
and inside vim, run:%!xxd -r
to edit a binary file using vim xxd filename > dump.hex
: Directly save the output to a file, which can be used withdiff
for comparison.