如果您从链接的页面使用此功能:
private 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");
}
并传入您的字符串,以 $ a 开头,以 * 结尾(在校验和之前),并在单词之间使用逗号,它可以工作。
string s1 = getChecksum("$GPGGA,102957.92,4104.8569,N,00836.4700,W,1,4,15.100,157.133,M,52.386,M,0,0*79");
string s2 = getChecksum("$GPRMC,102957.92,A,4104.8569,N,00836.4700,W,0.000,5.822,230211,0,W,A*2B");
如您所愿,s1 = 79 且 s2 = 2B。