Simvik

Two's-complement checksum

Compute the checksum byte of an Intel HEX record. An easy embedded C problem (Protocols, framing and checksums), run and graded in the browser.

easy · Protocols, framing and checksums · checksum · intel-hex

An Intel HEX record ends with a checksum chosen so that the sum of every byte in the record, including the checksum, is 0 modulo 256.

Return that checksum for the n bytes at data: the two's complement of the low byte of their sum.

Starter code

uint8_t ihex_checksum(const uint8_t *data, size_t n) {
  // (sum of data + result) & 0xFF must be 0.
  return 0;
}

Example tests

A data record

const uint8_t rec[] = { 0x03, 0x00, 0x30, 0x00, 0x02, 0x33, 0x7A };
printf("0x%02X\n", ihex_checksum(rec, sizeof rec));

Expected output:

0x1E

The end-of-file record

const uint8_t rec[] = { 0x00, 0x00, 0x00, 0x01 };
printf("0x%02X\n", ihex_checksum(rec, sizeof rec));

Expected output:

0xFF

Learn it first

More Protocols, framing and checksums problems