Simvik

CRC-8

Implement a bitwise CRC-8 with polynomial 0x07. A medium embedded C problem (Protocols, framing and checksums), run and graded in the browser.

medium · Protocols, framing and checksums · crc

Compute the CRC-8 of the n bytes at data with these parameters: polynomial 0x07, initial value 0x00, most significant bit first, no final XOR.

For each byte: XOR it into the CRC, then repeat eight times: if the top bit of the CRC is set, shift left and XOR with the polynomial; otherwise just shift left.

Starter code

uint8_t crc8(const uint8_t *data, size_t n) {
  uint8_t crc = 0x00;
  // For each byte: crc ^= byte, then 8 shift-and-conditionally-XOR steps.
  return crc;
}

Example tests

Standard check value

printf("0x%02X\n", crc8((const uint8_t *)"123456789", 9));

Expected output:

0xF4

Single byte

const uint8_t d[] = { 0x01 };
printf("0x%02X\n", crc8(d, 1));

Expected output:

0x07

Learn it first

More Protocols, framing and checksums problems