Safe Penetration Testing Labs · beginner · ~10 min

Render an ISO/IEC 14443 UID as hex

Format a contactless-card UID byte array as a colon-separated hex string.

Overview

Validate n, walk the byte array, emit %02X then : (except after the last byte), NUL.

Why it matters

The UID format is the lingua franca of every NFC log. Getting the render right means your logs are diffable against any other tool.

Lesson

Why this matters

ISO/IEC 14443 is the standard behind every contactless card — transit passes, hotel keys, NFC tags. The UID is 4, 7, or 10 bytes. Every NFC tool prints it the same way: AA:BB:CC:DD.

This exercise is the formatter side. Reading a UID from a real PN532 or PN5180 reader is a separate firmware project.

Your job

Implement int format_uid(const uint8_t *uid, size_t n, char *out, size_t cap). Render uid into out as uppercase hex, colon-separated, NUL- terminated. Return the number of bytes written (excluding NUL).

Rules:

  • n must be 4, 7, or 10. Anything else → -1.
  • Output length is n * 3 - 1 bytes (each byte is 2 hex chars + a separator, minus the trailing one). Plus 1 for NUL.
  • Return -1 if cap is too small.

Common mistakes

  • Using sprintf with %02x and getting lowercase. Use %02X or format by hand.
  • Off-by-one on the separator count.
  • Forgetting to validate the length.

What this is NOT

  • A reader driver. We don't talk to any hardware.
  • A clone tool. We render; we don't write.

Summary

Three valid lengths, bounded output, uppercase hex with colon separators.

Practice with these exercises