[Checksum] get XOR

Calculate checksum in C# in NMEA style.

You can use this code for either decoding and checking for instance GPS information (GPGGA for example) , or make your own checksum and append it to a string.

The idea is that your sentence has the form:
$$blablablabla*
then it will append the checksum (two chars) behind this *. Then you should just past a \n\r behind the sentence.

 

// Calculates the checksum for a line
public string GetXORChecksum(string sentence)
{
// Go thourgh characters
int Checksum = 0;
foreach (char Character in sentence)
{
if (Character == '$')
{
}
else if (Character == '*')
{
break;
}
else
{
if (Checksum == 0)
{
Checksum = Convert.ToByte(Character);
}
else
{
Checksum = Checksum ^ Convert.ToByte(Character);
}
}
}
// Return the result (checksum)
return Checksum.ToString("X2");
}

Advertisements

One response

24 03 2011
More code released « Tim Zaman

[…] [Checksum] get XOR […]

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s




%d bloggers like this: