crc校验码代码C#代码
时间: 2023-07-28 07:07:57 浏览: 222
以下是一份在 C# 中实现 CRC 校验的代码示例:
```csharp
public static byte[] CalculateCRC(byte[] data, ushort polynomial)
{
ushort[] table = new ushort[256];
ushort initialValue = 0;
ushort finalXORValue = 0;
// Initialize table
for (ushort i = 0; i < table.Length; ++i)
{
ushort temp = i;
for (byte j = 0; j < 8; ++j)
{
if ((temp & 0x0001) != 0)
{
temp = (ushort)((temp >> 1) ^ polynomial);
}
else
{
temp >>= 1;
}
}
table[i] = temp;
}
// Calculate CRC
ushort crc = initialValue;
for (int i = 0; i < data.Length; ++i)
{
byte index = (byte)(crc ^ data[i]);
crc = (ushort)((crc >> 8) ^ table[index]);
}
crc ^= finalXORValue;
// Return result as byte array
byte[] result = new byte[2];
result[0] = (byte)(crc & 0xFF);
result[1] = (byte)((crc >> 8) & 0xFF);
return result;
}
```
该方法接受两个参数:要计算 CRC 校验的字节数组和一个表示多项式的 16 位无符号整数。它会返回一个 2 字节的 byte 数组,其中包含计算得出的 CRC 校验值。
阅读全文
相关推荐
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![doc](https://img-home.csdnimg.cn/images/20241231044833.png)
![application/msword](https://img-home.csdnimg.cn/images/20210720083327.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![application/x-rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)