cybersecurity · beginner · ~10 min · safe pentest lab
Bounded hex-string rendering with strict length validation.
Format a contactless-card UID as colon-separated hex — the universal log format for NFC tools, so your logs diff against everyone else's.
Implement int format_uid(const uint8_t *uid, size_t n, char *out, size_t cap) that renders uid as uppercase hex separated by colons, NUL-terminating, and returns the number of bytes written (excluding the NUL).
uid, n: a fixture UID byte buffer and its length, baked into the harness. n must be 4, 7, or 10.out, cap: the output buffer and its capacity.Returns int: the number of characters written (n * 3 - 1) on success, or -1 on failure.
{0xAA, 0xBB, 0xCC, 0xDD}, n 4 -> out = "AA:BB:CC:DD", returns 11
n = 5 -> -1
n other than 4, 7, or 10 returns -1.cap == 0, or cap too small (need n*3 bytes including the NUL) returns -1.: between bytes but not after the last (watch the off-by-one).The colon-hex UID is the universal log format for NFC tools. Rendering it the same way means your logs diff against anyone else's.
A byte array of length 4, 7, or 10.
A NUL-terminated colon-hex string in out.
Uppercase hex. Length must be 4, 7, or 10.
#include <stdint.h>
#include <stddef.h>
int format_uid(const uint8_t *uid, size_t n, char *out, size_t cap) {
/* TODO */
(void)uid; (void)n; (void)out; (void)cap;
return -1;
}
Emitting : after the last byte (trailing separator). Using %02x (lowercase). Forgetting to validate n.
Exact-fit cap. NULL inputs. Length 5 (invalid).
O(n).
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.