Simvik

Format a hex byte

Write a byte as two upper-case hex characters without printf. An easy embedded C problem (Arrays and strings), run and graded in the browser.

easy · Arrays and strings · formatting · hex

Write v to out as exactly two upper-case hexadecimal characters followed by a terminating NUL. out has room for three bytes.

A bootloader that fits in 2 KB cannot afford printf, so do it by hand.

Starter code

void byte_to_hex(uint8_t v, char *out) {
  // 0x3C -> "3C" (two characters and a terminator).
  out[0] = '\0';
}

Example tests

Two digits

char o[3];
byte_to_hex(0x3C, o);
printf("%s\n", o);

Expected output:

3C

Letter digits

char o[3];
byte_to_hex(0xA0, o);
printf("%s\n", o);

Expected output:

A0

Learn it first

  • Arrays and strings · Decay, bounds, safe copying, and parsing and formatting without the heap.

More Arrays and strings problems