When working with binary files, developers often need a way to read data in a human-friendly format. Two common tools used for this task are hexdump
and xxd
. Both convert binary content into hexadecimal and ASCII representations, but their output style, structure, and options differ.
In Linux, Unix, and macOS environments, these commands help inspect raw file data, debug corrupted files, or analyze unknown binaries. Knowing their differences helps you pick the right one for your task.
What is Hexdump?
hexdump
is a traditional Unix utility that displays binary file content in hexadecimal, octal, decimal, or ASCII format. It’s part of most Unix-based systems and often pre-installed. The tool reads a file byte by byte and prints an address offset, followed by a set of hex values and the corresponding ASCII characters.
A basic command looks like this:
hexdump -C file.bin
This option (-C
) produces a canonical display similar to what’s seen in many file analysis tools. The left column shows byte offsets, the middle shows hexadecimal bytes, and the right column prints readable ASCII characters when possible.
Developers use hexdump
for debugging, comparing data streams, checking file signatures, or reviewing file structure at a low level. It’s script-friendly, works well with pipelines, and supports flexible output formats.
What is xxd?
xxd
is another command-line utility that generates a hex dump from a binary file. It also supports the reverse operation — converting a hex dump back into its original binary form. This feature makes it popular among programmers and reverse engineers.
Example usage:
xxd file.bin
The output includes the byte offset on the left, followed by groups of hexadecimal pairs and an ASCII column. The format is clean and consistent across systems. Unlike hexdump
, xxd
comes bundled with the Vim text editor and integrates directly into it. You can open a file in Vim, use :%!xxd
to view it as hex, then revert back using :%!xxd -r
.
This ability to toggle between formats is what makes xxd
powerful for file modification and binary inspection.
Hexdump vs xxd: Format and Function Comparison
Both tools display hex and ASCII data, but they do it differently. Here’s a quick breakdown:
Feature | Hexdump | xxd |
---|---|---|
Default Output | Compact, variable format | Fixed column layout |
Readability | Moderate | High |
Reverse Conversion | Not supported | Supported (xxd -r ) |
ASCII Column | Optional | Always shown |
Tool Origin | Unix/POSIX standard | Part of Vim package |
Output Customization | Flexible flags | Limited but consistent |
The main difference lies in readability and flexibility. xxd
produces cleaner, more readable output by default, while hexdump
offers more control through formatting flags. Developers who need consistent column spacing often prefer xxd
. System engineers who want customized formatting for scripts lean toward hexdump
.
Example: Comparing Both Commands on the Same File
Take a simple text file as an example. Running both commands shows their output format difference clearly:
hexdump -C sample.txt
Output might look like this:
00000000 48 65 6c 6c 6f 20 77 6f 72 6c 64 0a |Hello world.|
Now try:
xxd sample.txt
Output example:
00000000: 4865 6c6c 6f20 776f 726c 640a Hello world.
Both represent the same data, but xxd
uses a compact and consistent layout, while hexdump
leaves variable spacing between byte groups. For quick human reading, xxd
feels cleaner. For automated text parsing, hexdump
offers better control with flags and scripting.
Practical Use Cases
Both tools serve slightly different purposes depending on what you’re doing.
- Developers use them to inspect binary data, verify file integrity, and check raw content during debugging.
- Security analysts rely on them to analyze malware payloads or look at suspicious binary code.
- System administrators use them for troubleshooting configuration files or data corruption.
- Reverse engineers prefer
xxd
because they can convert hex dumps back into binary withxxd -r
.
Each command supports piping and redirection, which means they integrate easily into shell scripts and automation workflows.
When to Use Which Tool
Use hexdump
when you need flexible formatting, automation in shell scripts, or specific output control. It’s built into most Unix systems and supports advanced options like grouping bytes, changing endianness, and customizing format strings.
Use xxd
when you need consistent, visually clean hex output or when you want to reverse a hex dump back into binary data. It’s ideal for inspecting small files or working inside Vim.
Both tools produce nearly identical results, but choosing one depends on whether you value readability or customization.
Conclusion
hexdump
and xxd
both reveal what’s hidden inside binary files, but their approaches differ. hexdump
gives more flexibility and control for scripting and automation, while xxd
offers simplicity and reversibility.
If you need to analyze files visually, xxd
is easier to read. If you’re building command pipelines or need structured formats, hexdump
is the better fit.
Both remain valuable tools for developers, analysts, and anyone curious about what’s behind a file’s binary data.