Hello all. Wondering if anyone was successful in creating checking checksums in NMEA messages? Problem I am having is that .net micro does not have a Convert class... Has anyone on the forum been successfull in calculating NMEA checksums?
http://www.codepedia... NMEA Checksums
This is my first kick at writing a function to check the checksums of a NMEA message.. Its a bit long because as far as I know, there are no methods in .NET MICRO to convert to hex strings from an integer. if there are any ways to make this code more efficient please let me know. It should also be noted that there is little to no error handling
class NMEAparser { public bool varifyCheckSum(String Message) { int index = 0; string checksum; index = Message.IndexOf('*', 1); if (index == -1) return false; byte[] byteArray = System.Text.Encoding.UTF8.GetBytes(Message.Substring(1, index - 1)); checksum = Message.Substring(index + 1, 2); int decimalValue = 0; foreach (byte b in byteArray) { if ( decimalValue == 0) { decimalValue = int.Parse(b.ToString()); } else { decimalValue = decimalValue ^ int.Parse(b.ToString()); } } String calcCheckSum = intToHexString(decimalValue); return calcCheckSum.Substring(calcCheckSum.Length - 2) == checksum; } public string intToHexString(int value) { string hexString = ""; if (value == 0) { hexString = "0"; } else { switch (value%16) { case 0: hexString = intToHexString(value / 16) + "0"; break; case 1: hexString = intToHexString(value / 16) + "1"; break; case 2: hexString = intToHexString(value/16) + "2"; break; case 3: hexString = intToHexString(value / 16) + "3"; break; case 4: hexString = intToHexString(value / 16) + "4"; break; case 5: hexString = intToHexString(value / 16) + "5"; break; case 6: hexString = intToHexString(value / 16) + "6"; break; case 7: hexString = intToHexString(value / 16) + "7"; break; case 8: hexString = intToHexString(value / 16) + "8"; break; case 9: hexString = intToHexString(value / 16) + "9"; break; case 10: hexString = intToHexString(value / 16) + "A"; break; case 11: hexString = intToHexString(value / 16) + "B"; break; case 12: hexString = intToHexString(value / 16) + "C"; break; case 13: hexString = intToHexString(value / 16) + "D"; break; case 14: hexString = intToHexString(value / 16) + "E"; break; case 15: hexString = intToHexString(value / 16) + "F"; break; } } return hexString; } }