cybersecurity · beginner · ~15 min
A reusable hex-dump primitive.
Implement int hex_dump_line(unsigned long offset, const unsigned char *bytes, int n, char *out, int cap).
Format: OFFSET: HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH HH ASCII
(3 spaces) instead of HH .>= 0x20 && < 0x7f), else ..n is the count of bytes (0..16). Returns bytes written (excluding NUL),
or -1 if would not fit.
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.
offset + bytes + count + output + cap.
Bytes written (or -1).
Use snprintf or hand-roll; bound everything.
#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.