# How to calculate CRC 8 Check-Sum using C#.

Posted by in C# category on for Beginner level | Points: 250 | Views : 21816

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 code

We 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++;
}
lblMsg.Text = Convert.ToString(_checkSumData);
}
else
lblMsg.Text = "Invalid checksum data.";
}
catch
{
lblMsg.Text = "Invalid checksum data.";
}
}```

## Conclusion

Full Name: MAHESH ALLE
Member Level: Silver
Member Status: Member,MVP
Member Since: 4/2/2012 1:34:42 AM
Country: India

Login to vote for this post.