Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Creating a Hex Dump of a File
A hex dump displays the binary contents of a file in hexadecimal format, making it readable for humans. This is essential for programming, reverse engineering, data recovery, and debugging executable files. Linux provides several built-in tools to generate hex dumps of files.
To demonstrate these tools, let's first create a sample text file
$ cat > example.txt This is our sample text in the file. We will convert it into hex using various tools. ^D
Using hexdump Command
The hexdump command is a built-in Linux utility that displays file contents in hexadecimal, decimal, octal, or ASCII formats. It's particularly useful for inspecting executable code and data recovery.
Basic Syntax
$ hexdump [options] [files]
Default Hex Output
By default, hexdump displays data as 16-bit hexadecimal numbers
$ hexdump example.txt
0000000 6854 7369 6920 2073 756f 2072 6173 706d 0000010 656c 7420 7865 2074 6e69 7420 6568 6620 0000020 6c69 2e65 570a 2065 6977 6c6c 6320 6e6f 0000030 6576 7472 6920 2074 6e69 6f74 6820 7865 0000040 7520 6973 676e 7620 7261 6f69 7375 7420 0000050 6f6f 736c 0a2e 0000056
Canonical Format with ASCII
Use the -C flag to display both hex and ASCII data side by side
$ hexdump -C example.txt
00000000 54 68 69 73 20 69 73 20 6f 75 72 20 73 61 6d 70 |This is our samp| 00000010 6c 65 20 74 65 78 74 20 69 6e 20 74 68 65 20 66 |le text in the f| 00000020 69 6c 65 2e 0a 57 65 20 77 69 6c 6c 20 63 6f 6e |ile..We will con| 00000030 76 65 72 74 20 69 74 20 69 6e 74 6f 20 68 65 78 |vert it into hex| 00000040 20 75 73 69 6e 67 20 76 61 72 69 6f 75 73 20 74 | using various t| 00000050 6f 6f 6c 73 2e 0a |ools..| 00000056
Using xxd Command
The xxd utility creates hex dumps and can also reverse hex dumps back to their original binary format. It provides a clean, readable output format.
Syntax
$ xxd [options] [file]
Example
$ xxd example.txt
00000000: 5468 6973 2069 7320 6f75 7220 7361 6d70 This is our samp 00000010: 6c65 2074 6578 7420 696e 2074 6865 2066 le text in the f 00000020: 696c 652e 0a57 6520 7769 6c6c 2063 6f6e ile..We will con 00000030: 7665 7274 2069 7420 696e 746f 2068 6578 vert it into hex 00000040: 2075 7369 6e67 2076 6172 696f 7573 2074 using various t 00000050: 6f6f 6c73 2e0a ools..
Using od Command
The od (octal dump) command converts files to different formats. While it defaults to octal format, we can specify hexadecimal output using the -t x1 flag.
Hexadecimal Output
$ od -t x1 example.txt
0000000 54 68 69 73 20 69 73 20 6f 75 72 20 73 61 6d 70 0000020 6c 65 20 74 65 78 74 20 69 6e 20 74 68 65 20 66 0000040 69 6c 65 2e 0a 57 65 20 77 69 6c 6c 20 63 6f 6e 0000060 76 65 72 74 20 69 74 20 69 6e 74 6f 20 68 65 78 0000100 20 75 73 69 6e 67 20 76 61 72 69 6f 75 73 20 74 0000120 6f 6f 6c 73 2e 0a 0000126
Combined Hex and Character Display
Combine -t x1 and -c flags to show both hex values and character representation
$ od -t x1 -c example.txt
0000000 54 68 69 73 20 69 73 20 6f 75 72 20 73 61 6d 70 T h i s i s o u r s a m p 0000020 6c 65 20 74 65 78 74 20 69 6e 20 74 68 65 20 66 l e t e x t i n t h e f 0000040 69 6c 65 2e 0a 57 65 20 77 69 6c 6c 20 63 6f 6e i l e .
W e w i l l c o n 0000060 76 65 72 74 20 69 74 20 69 6e 74 6f 20 68 65 78 v e r t i t i n t o h e x 0000100 20 75 73 69 6e 67 20 76 61 72 69 6f 75 73 20 74 u s i n g v a r i o u s t 0000120 6f 6f 6c 73 2e 0a o o l s .
0000126
Comparison of Tools
| Tool | Best For | Key Features |
|---|---|---|
| hexdump | General-purpose hex viewing | Multiple output formats, canonical mode |
| xxd | Clean readable output | Reversible, consistent formatting |
| od | Multiple format conversion | Octal/hex/character display options |
Conclusion
Linux provides multiple tools for creating hex dumps, each with unique advantages. The hexdump command offers versatile formatting options, xxd provides clean readable output with reversibility, and od excels at multiple format conversions. These tools are essential for debugging, reverse engineering, and analyzing binary file contents.
