Simvik

BCD conversion

Convert between binary-coded decimal and binary. An easy embedded C problem (Protocols, framing and checksums), run and graded in the browser.

easy · Protocols, framing and checksums · bcd · rtc

Real-time clock chips store time in binary-coded decimal: the tens digit in the high nibble and the units digit in the low nibble, so 59 seconds is 0x59.

Write both conversions for values from 0 to 99.

Starter code

uint8_t bcd_to_bin(uint8_t bcd) {
  // 0x59 -> 59
  return bcd;
}

uint8_t bin_to_bcd(uint8_t value) {
  // 59 -> 0x59
  return value;
}

Example tests

BCD to binary

printf("%u %u\n", bcd_to_bin(0x59), bcd_to_bin(0x07));

Expected output:

59 7

Binary to BCD

printf("0x%02X 0x%02X\n", bin_to_bcd(59), bin_to_bcd(7));

Expected output:

0x59 0x07

Learn it first

More Protocols, framing and checksums problems