Hexdump Tool
View File Contents
The hexdump command is a powerful utility for viewing
the contents of files in a hexadecimal, decimal, octal, or ASCII
format. It's invaluable for debugging, reverse engineering, and
understanding binary data structures.
Common Hexdump Usage Examples
Here are some practical examples of how to use the
hexdump command:
Side-by-Side Hexadecimal and ASCII View
To display the first 128 bytes of a file in a side-by-side
hexadecimal and ASCII representation, use the -C option
for canonical hex+ASCII display and -n to specify the
number of bytes:
# side-by-side hexadecimal and ASCII view of the first 128 bytes of a file
hexdump -C -n128 /etc/passwd
Convert Binary File to C Array
You can convert a binary file into a C array format, which is useful
for embedding binary data directly into C/C++ programs. The
-v option prevents output from being abridged, and the
-e option specifies the output format:
# Convert a binary file to C Array
hexdump -v -e '16/1 "0x%02X, "' -e '"\n"' file.bin > hexarray.h
Convert Binary File to Shell Code
For generating shell code, you can use a specific format string with
hexdump:
# Convert a binary file to Shell code
hexdump -v -e '"\\""x" 1/1 "%02x" """'
Generate Random MAC Address
hexdump can also be used to generate random data, such
as a MAC address. By reading from /dev/random and
formatting the output:
# Generate random MAC address
hexdump -n6 -e '/1 ":%02X"' /dev/random|sed s/^://g
Understanding Hexdump Options
-C: Canonical hex+ASCII display.-
-n <bytes>: Display only the first<bytes>bytes of the input. -v: Do not abridge the output.-
-e <format_string>: Use the specified format string for output.