How to calculate CRC 8 Check-Sum using C# code.
Introduction
We are going to check the code that will calculate CRC 8 Check Sum of Hex Data.
Using the codeWe are going to look into ComputeAdditionChecksum() function. This function will take the data bytes. Then these data bytes are then processed one at a time and CRC 8 will be calculated.
Below is the code:
public static byte ComputeAdditionChecksum(byte[] data)
{
byte sum = 0;
unchecked // Let overflow occur without exceptions
{
foreach (byte b in data)
{
sum += b;
}
}
return sum;
}
Below is the code for UI. Here text box is used to get the hex data form the user. The entered data by user will be stored in byte array using for loop. After that byte array is passed to
ComputeAdditionChecksum()
function.
Here is the code.
protected void btnSybmit_Click(object sender, EventArgs e)
{
try
{
if (txtData.Text.Length % 2 == 0)
{
byte[] _crcdata = new byte[txtData.Text.Length / 2];
int k = 0;
for (int i = 0; i < txtData.Text.Length; i = i + 2)
{
_crcdata[k] = byte.Parse(txtData.Text.Substring(i, 2).ToString(), System.Globalization.NumberStyles.AllowHexSpecifier);
k++;
}
byte _checkSumData = Crc8.ComputeAdditionChecksum(_crcdata);
lblMsg.Text = Convert.ToString(_checkSumData);
}
else
lblMsg.Text = "Invalid checksum data.";
}
catch
{
lblMsg.Text = "Invalid checksum data.";
}
}
Demo

Conclusion
Hope you will enjoy this article. Please provide the suggestions on this article in comments section.