Skip to content

Instantly share code, notes, and snippets.

@techsd
Forked from maxp/NMEA_checksum
Created July 28, 2023 20:33
Show Gist options
  • Save techsd/13dd3884ccf6bffa563a09c96a3084e5 to your computer and use it in GitHub Desktop.
Save techsd/13dd3884ccf6bffa563a09c96a3084e5 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)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment