XXD Command Explained: Convert Files to Hex and Back with Examples

XXD Command Explained: Convert Files to Hex and Back with Examples

If you have ever opened a binary file and seen a wall of strange characters, you have met the problem that xxd was built to solve. Available on many Unix-like systems, especially those with Vim installed, xxd converts files into a readable hexadecimal representation and can also convert that hex dump back into the original binary data. It is a small command, but it is extremely useful for debugging, reverse engineering, inspecting file headers, editing binary data, and understanding what a file really contains.

TLDR: xxd is a command-line tool that turns files into hexadecimal dumps and can recreate files from those dumps. It is commonly used to inspect binary files, compare raw data, patch bytes, and generate C-style arrays. The most common command is xxd file, while xxd -r dump.hex outputfile converts a hex dump back into binary form.

What Is the XXD Command?

The xxd command creates a hex dump, which is a human-readable view of binary data. Computers store files as bytes, and each byte can be represented by two hexadecimal digits. Instead of trying to display those bytes as text, xxd shows them in a clean layout with offsets, hexadecimal values, and an ASCII preview.

A simple example looks like this:

xxd hello.txt

If hello.txt contains the text Hello, the output might be:

00000000: 4865 6c6c 6f0a                           Hello.

Here, 00000000 is the byte offset, 4865 6c6c 6f0a is the hexadecimal representation, and Hello. is the ASCII preview. The dot represents a non-printable character, in this case the newline character.

Why Use XXD?

At first glance, a hex dump may seem like something only a low-level programmer would need. In reality, xxd is useful in several everyday technical situations:

  • Inspecting file headers: Identify file types by looking at their magic bytes.
  • Debugging binary protocols: See exactly what bytes are being sent or received.
  • Recovering or patching files: Modify a small part of a binary file without opening a complex editor.
  • Comparing files: View subtle byte-level differences between two files.
  • Embedding files in C programs: Generate C arrays from binary data.

For example, PNG images usually begin with a recognizable signature. Running xxd image.png | head may reveal bytes such as 8950 4e47, which correspond to the PNG file format.

Basic XXD Syntax

The basic syntax is straightforward:

xxd [options] inputfile [outputfile]

If you do not provide an output file, xxd prints the result to standard output. If you provide one, it writes the hex dump there:

xxd input.bin output.hex

This creates a text file named output.hex containing the hex representation of input.bin. Because the dump is plain text, you can open it in a text editor, email it, paste it into bug reports, or store it in version control.

Convert a File to Hex

To convert any file to a standard hex dump, run:

xxd sample.bin

To save the dump:

xxd sample.bin sample.hex

You can control how many bytes appear per line with -c. For example, to show 8 bytes per row:

xxd -c 8 sample.bin

This is helpful when you want a narrower output or when analyzing structured data where fields appear in fixed widths.

Convert Hex Back to a File

One of the most powerful features of xxd is reverse conversion. If you have a valid hex dump, you can recreate the original file using the -r option:

xxd -r sample.hex restored.bin

If sample.hex was generated from sample.bin, then restored.bin should match the original file exactly. You can verify this with:

cmp sample.bin restored.bin

If there is no output, the files are identical. This round-trip ability makes xxd excellent for making small manual changes to binary files. You can dump the file, edit a few hex bytes, and then reverse the dump back into a binary file.

Create a Plain Hex String

Sometimes you do not want offsets or ASCII columns. You only want the raw hexadecimal characters. Use the -p option, also known as plain mode:

xxd -p file.bin

For a text file containing Hello, this might output:

48656c6c6f0a

Plain mode is useful for scripts, checksums, network testing, and copying byte sequences into other tools. To reverse plain hex back into binary, combine -r and -p:

xxd -r -p hexstring.txt output.bin

You can also pipe values directly:

echo 48656c6c6f0a | xxd -r -p

This prints Hello followed by a newline.

Limit the Number of Bytes

Large files can produce huge dumps, so you may want to inspect only a small portion. The -l option limits the number of bytes displayed:

xxd -l 64 largefile.bin

This shows only the first 64 bytes. To start at a specific position, use -s:

xxd -s 128 -l 64 largefile.bin

This skips the first 128 bytes and then displays the next 64 bytes. These two options are extremely useful when analyzing file formats, because many important structures are located at known offsets.

Generate C Arrays with XXD

Another handy feature is C include output. With -i, xxd converts a file into a C-style unsigned char array:

xxd -i logo.png

The output looks similar to this:

unsigned char logo_png[] = {
  0x89, 0x50, 0x4e, 0x47, ...
};
unsigned int logo_png_len = 1234;

This is useful when embedding small binary resources directly into a C or C++ program. For example, firmware developers may embed configuration data, icons, certificates, or test payloads this way.

Editing Bytes with XXD

A practical workflow for patching binary data looks like this:

  1. Create a dump: xxd program.bin program.hex
  2. Open program.hex in a text editor.
  3. Modify the needed hexadecimal bytes carefully.
  4. Rebuild the binary: xxd -r program.hex patched.bin
  5. Compare or test the patched file.

Be careful when editing the offset column or ASCII preview. In a normal xxd dump, the meaningful byte values are in the hexadecimal section. Changing the ASCII preview does not necessarily change the resulting binary data. For reliable editing, modify only the hex bytes and keep the structure of the dump intact.

Common XXD Options

Here are some useful options worth remembering:

  • -r: Reverse a hex dump back into binary.
  • -p: Use plain continuous hex output.
  • -c N: Display N bytes per line.
  • -l N: Limit output to N bytes.
  • -s N: Seek to offset N before dumping.
  • -i: Output as a C include array.
  • -g N: Group bytes by N bytes in the display.

XXD vs Hex Editors

A graphical hex editor is better when you need interactive navigation, search tools, and visual editing. However, xxd shines because it is scriptable, lightweight, fast, and available in terminal environments. It fits naturally into pipelines with commands like head, tail, grep, diff, and cmp.

For example, comparing the first few bytes of two files is easy:

xxd -l 32 file1.bin
xxd -l 32 file2.bin

Or you can save both dumps and compare them:

xxd file1.bin > file1.hex
xxd file2.bin > file2.hex
diff file1.hex file2.hex

Final Thoughts

The xxd command may look simple, but it opens a direct window into the byte-level structure of files. Whether you are diagnosing corrupted data, learning how file formats work, embedding assets in source code, or converting hex back into binary, it provides a quick and dependable toolkit. Once you become comfortable reading offsets, hex bytes, and ASCII previews, xxd turns mysterious binary files into something you can inspect, understand, and even carefully modify.