Simvik

Validate an NMEA sentence

Check the checksum of a GPS sentence. A medium embedded C problem (Protocols, framing and checksums), run and graded in the browser.

medium · Protocols, framing and checksums · parsing · checksum · gps

A GPS receiver sends lines such as $GPGLL,5300.97914,N,00259.98174,E,125926,A*28. The two hex digits after * are the XOR of every character between $ and *, not including either.

Return true if s starts with $, contains a * followed by exactly two hex digits (upper or lower case) and those digits match the computed checksum. Anything after the two digits, such as a line ending, is ignored.

Starter code

bool nmea_valid(const char *s) {
  // XOR the characters between '$' and '*', then compare with the two hex digits.
  return false;
}

Example tests

A valid sentence

printf("%d\n", nmea_valid("$GPGLL,5300.97914,N,00259.98174,E,125926,A*28"));

Expected output:

1

A corrupted character

printf("%d\n", nmea_valid("$GPGLL,5300.97914,S,00259.98174,E,125926,A*28"));

Expected output:

0

Learn it first

More Protocols, framing and checksums problems