cybersecurity · beginner · ~15 min
A reusable hex-dump primitive.
Format one row of an xxd-style hex dump — the universal byte-level view used in forensics.
Implement int hex_dump_line(unsigned long offset, const unsigned char *bytes, int n, char *out, int cap) that writes one formatted line into out.
Line format:
OFFSET: HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH ASCII
OFFSET: 8 lowercase hex digits followed by : .HH (lowercase hex); missing slots (n < 16) render as 3 spaces.n bytes renders as itself if printable (>= 0x20 && < 0x7f), else ..offset: the row's starting offset.bytes, n: the row data and its byte count (0 <= n <= 16). The grader passes fixed buffers.out, cap: destination buffer and its size.Returns the number of bytes written (excluding the NUL terminator), or -1 if the line would not fit in cap.
offset 0x10, 16 bytes "Hello!" + binary -> line containing "00000010:" and gutter "Hello!.."
offset 0, bytes {0x41,0x42,0x43}, n=3 -> line containing "00000000:" and gutter "ABC"
cap too small (e.g. 20) -> -1
n == 0: an empty row (offset, blank slots, no gutter characters).n == 16: a full row.. in the gutter.cap; return -1 rather than overflow.A hex dump is the universal byte-level debugger. Knowing how to lay one out in 16-byte rows with an ASCII gutter is a daily forensic skill.
An offset, a byte buffer bytes with count n (0..16), a destination out, and its size cap.
Bytes written excluding NUL, or -1 if the line would not fit in cap.
Bound every write to cap; missing slots are 3 spaces; gutter prints 0x20..0x7e or ..
#include <stddef.h>
int hex_dump_line(unsigned long offset, const unsigned char *bytes, int n, char *out, int cap) { /* TODO */ (void)offset; (void)bytes; (void)n; (void)out; (void)cap; return -1; }
Treating non-ASCII as printable. Off-by-one on the row of 16.
n == 0 (empty row). n == 16 (full row).
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.