Creating a Hex Dump of a File


Introduction

Hexdump shows the binary file contents in hexadecimal, decimal, octal, or ASCII form. It is an inspection tool that is also useful for programming, reverse engineering, and data recovery. It represents the content of a file in hexadecimal form. The various tools we can use to generate a file's hex dump will be covered in this article. We will learn to create a hex dump of a file.

To utilise as a model for this article, let's generate an ASCII text file.

$ cat >> example.txt

This is our sample text in the file.

We will convert it into hex using various tools.

By Using hexdump

Linux comes with a built-in tool called hexdump that allows you to filter and view the contents of various files in hex, decimal, octal, or ASCII formats. It works effectively as an examination tool and has data recovery capabilities.Additionally, we can use it to inspect the executable code of various programmes.

hexdump's fundamental syntax is as follows −

Syntax

$ hexdump [options…] [files …]

Example

Let's produce a hex dump from the example.txt file we created earlier −

$ hexdump example.txt

Output

The hexdump command outputs hex data as 16-bit numbers by default.

$ 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 $

If you want to show the hex data as well as ASCII data, you can use the same command with canonical flag. Same command will be written as −

Example

$ hexdump C example.txt

Output

$ 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 $

By Using xxd

We can use xxd to generate a file's hex dump. Additionally, it can be used to return a hex dump to its original ASCII or binary format.

Syntax

Xxd has the following syntax.

$ xxd [options …] [file …]

Example

Lets run the command −

$ xxd example.txt

Output

$ 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..
$

By Using od

od, also known as octal dump, is a built-in command for converting files to different formats. The octal format is the default.

The od command can accept multiple files as input by combining the data in each of the files listed in the order they were listed.

With the aid of this command, we may convert executable files into a format that can be read by humans and used for debugging.

As we know od command shows the data in octal format by default, the output will be converted to hex format using the -t x1 flag −

Example

$ od -t x1 example.txt

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

Additionally, we can use the -t x1 flag combined with the -c flag to show each character's hex value −

Example

$ od -t x1 -c example.txt

Output

$ 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 $

Conclusion

The various tools for converting files to hex format have been covered in this article. Editing hex files is especially crucial when working with executable files and debugging purposes. In this tutorial, we have examined various tools to create a hex dump of a file.

Updated on: 21-Nov-2022

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements