-
-
Save techsd/13dd3884ccf6bffa563a09c96a3084e5 to your computer and use it in GitHub Desktop.
Calculating an NMEA Checksum (C#)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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