Post edited 8:37 PM – February 6, 2012 by Haz
This is what I did to calculate the checksum of the data in C#. Given that my program handled all data in String formatting first, I first made sure I remove custom formatting then convert to byte array.
private string RFID_1356MHZ_Calculate_CSUM(string CommandBuilder)
{
byte CSUM = 0; // strip the input of all delimiter characters and the optional 0x prefix
string data = CommandBuilder.Replace("0x", "").Replace("0X", "").Replace("/n", "0A").Replace("/r","0D").Replace(delimiter, "").Replace(",", "").Replace(" ", "").ToUpper(); // convert the hex characters to a byte array byte[] buffer = new byte[data.Length / 2 + lineEnding.Length];
for (int i = 0; i < data.Length; i += 2)
{
buffer[i / 2] = byte.Parse(data.Substring(i, 2), System.Globalization.NumberStyles.AllowHexSpecifier);
}
//Calculate CheckSum
unchecked // Let overflow occur without exceptions
{
for(int i = 0; i < buffer.Length; i++)
{
CSUM ^= buffer[i];
}
}
return (String.Format("{0}{1:X2}{2}", hexPrefix, CSUM, delimiter));
}