cybersecurity · intermediate · ~15 min · safe pentest lab

UID bytes to hex

Hex-encode a byte buffer.

Challenge

Hex-encode a card UID — RFID/NFC UIDs are logged and compared as hex strings.

Task

Implement int uid_to_hex(const unsigned char *uid, int n, char *out, int outsz) that writes the lowercase hex of the n bytes of uid into out, NUL-terminating, and returns the number of hex characters written (n*2).

Input

  • uid: a byte buffer of length n. The grader passes a fixed buffer.
  • n: the number of bytes to encode.
  • out, outsz: the output buffer and its capacity.

Output

Returns int: n*2 on success (with out holding n*2 lowercase hex chars plus a NUL), or -1 if out cannot hold n*2 + 1 bytes.

Example

uid = {0x04, 0xA2, 0xBF}, n = 3, outsz = 8   ->   out = "04a2bf", returns 6
outsz = 4                                     ->   -1   (won't fit)

Edge cases

  • Need room for the trailing NUL: require outsz >= n*2 + 1.

Input format

A byte buffer uid of length n, an output buffer out, and its size outsz.

Output format

An int: n*2 on success (out is lowercase hex + NUL), or -1 if too small.

Constraints

Lowercase hex. Need outsz >= n*2+1 or return -1.

Starter code

int uid_to_hex(const unsigned char *uid, int n, char *out, int outsz) {
    /* TODO */
    return -1;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.