hexdump: Formatted Byte Dumps in CI
hexdump -C prints a canonical hex-plus-ASCII dump; custom format strings control any other layout.
hexdump ships with util-linux/bsdmainutils and gives the familiar hexdump -C view. Its format-string engine also builds one-off layouts a test needs.
What it does
hexdump reads bytes and formats them. -C is the canonical view (offset, 16 hex bytes, ASCII in |...|). Without a flag it defaults to a two-byte octal layout, which surprises people; almost everyone wants -C. Custom -e format strings emit any layout, e.g. one decimal value per line.
Common usage
# canonical hex + ASCII (what you usually want)
hexdump -C file.bin | head
# first 32 bytes only
hexdump -C -n 32 file.bin
# start at offset 512
hexdump -C -s 512 file.bin
# custom: one uppercase hex byte per line
hexdump -v -e '1/1 "%02X\n"' file.binOptions
| Flag | What it does |
|---|---|
| -C | Canonical hex + ASCII display |
| -v | Show all lines (do not collapse duplicates to "*") |
| -n <len> | Interpret only len bytes of input |
| -s <off> | Skip off bytes from the start |
| -e <fmt> | Custom iteration/format string |
| -b | One-byte octal display |
| -x | Two-byte hex display |
In CI
To extract a bare hex string for a checksum-free byte comparison, use hexdump -v -e "/1 \"%02x\"" file, which prints continuous lowercase hex with no offsets. Add -v any time you feed the output to a diff, or collapsed duplicate lines will mask real differences.
Common errors in CI
"hexdump: command not found" on slim images means util-linux/bsdmainutils is not installed (apt-get install -y bsdmainutils, apk add util-linux); use od -A x -t x1z as a fallback. Forgetting -C yields the default octal two-byte format and confuses readers expecting hex. On Alpine, BusyBox provides a reduced hexdump that may reject some -e format strings.