Created
September 4, 2011 17:44
-
-
Save maxp/1193206 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) | |
} |
iwaken71
commented
Jun 3, 2016
•
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