Skip to content

Instantly share code, notes, and snippets.

@maxp
Created September 4, 2011 17:44
Show Gist options
  • Save maxp/1193206 to your computer and use it in GitHub Desktop.
Save maxp/1193206 to your computer and use it in GitHub Desktop.
Calculating an NMEA Checksum (C#)
// Calculates the checksum for a sentence
// Calculates the checksum for a sentence
static string getChecksum(string sentence) {
//Start with first Item
int checksum= Convert.ToByte(sentence[sentence.IndexOf('$')+1]);
// Loop through all chars to get a checksum
for (int i=sentence.IndexOf('$')+2 ; i<sentence.IndexOf('*') ; i++){
// No. XOR the checksum with this character's value
checksum^=Convert.ToByte(sentence[i]);
}
// Return the checksum formatted as a two-character hexadecimal
return checksum.ToString("X2");
}
public bool IsValid(string sentence)
{
// Compare the characters after the asterisk to the calculation
return sentence.Substring(sentence.IndexOf("*") + 1) = GetChecksum(sentence)
}
@devendranaga
Copy link

A gps checksum validator in C here:

#include <stdio.h>
#include <string.h>

int nmea0183_checksum(char *nmea_data)
{
    int crc = 0;
    int i;

    // the first $ sign and the last two bytes of original CRC + the * sign
    for (i = 1; i < strlen(nmea_data) - 3; i ++) {
        crc ^= nmea_data[i];
    }

    return crc;
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment