cybersecurity · beginner · ~10 min · safe pentest lab
Bounded hex-string rendering with strict length validation.
Implement:
#include <stdint.h>
#include <stddef.h>
int format_uid(const uint8_t *uid, size_t n, char *out, size_t cap);
Render uid as uppercase hex separated by colons. Return the number
of bytes written (excluding NUL).
Rules:
n must be 4, 7, or 10. Anything else → -1.n * 3 - 1 bytes plus 1 for the NUL.cap == 0, or cap is too small.{0xAA, 0xBB, 0xCC, 0xDD} → "AA:BB:CC:DD", returns 11n = 5 → -1%02X; for all but the
last byte, emit a :.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.