C# calculation CRC16
Code for CRC116:
private static string Crc16Ccitt(byte[] bytes)
{
const ushort poly = 4129;
ushort[] table = new ushort[256];
ushort initialValue = 0xffff;
ushort temp, a;
ushort crc = initialValue;
for (int i = 0; i < table.Length; ++i)
{
temp = 0;
a = (ushort)(i << 8);
for (int j = 0; j < 8; ++j)
{
if (((temp ^ a) & 0x8000) != 0)
temp = (ushort)((temp << 1) ^ poly);
else
temp <<= 1;
a <<= 1;
}
table[i] = temp;
}
for (int i = 0; i < bytes.Length; ++i)
{
crc = (ushort)((crc << 8) ^ table[((crc >> 8) ^ (0xff & bytes[i]))]);
}
return crc.ToString();
}
Explanation by Chatgpt
This C# code defines a function `Crc16Ccitt`, which calculates the 16-bit CRC (Cyclic Redundancy Check) using the CCITT (Comité Consultatif International Téléphonique et Télégraphique) polynomial (0x1021) for a given input byte array.
Here's a step-by-step explanation of the code:
1. The function takes a byte array `bytes` as input.
2. It defines a constant `poly` with the value 4129 (0x1021), which represents the CCITT polynomial.
3. It initializes an array `table` of 256 elements to store precalculated values for faster CRC computation.
4. `initialValue` is set to 0xffff, which is the initial value for the CRC calculation.
5. `temp`, `a`, and `crc` are defined as unsigned 16-bit integers (ushort) for temporary storage and the final CRC result.
6. The first `for` loop initializes the `table` array by precomputing CRC values for all possible byte values (0 to 255).
- `temp` is set to 0.
- `a` is shifted left by 8 bits to make room for the current byte value.
- The second `for` loop runs 8 times, one bit at a time.
- In each iteration, it checks the most significant bit of the XOR between `temp` and `a`.
- If the XOR result has its most significant bit set (1), it means a carry will occur in the CRC calculation, and the polynomial is XORed with `temp`.
- The value of `temp` is then shifted left by 1 bit.
- The value of `a` is also shifted left by 1 bit to consider the next bit in the polynomial.
7. After the first loop, the `table` array is filled with precomputed CRC values for all 256 possible byte values.
8. The second `for` loop calculates the CRC value for the input byte array `bytes`.
- It iterates over each byte in the array.
- `crc` is shifted left by 8 bits to make room for the next byte's CRC calculation.
- The current byte is XORed with the least significant byte of `crc`.
- The result is used as an index to look up the precomputed CRC value from the `table` array.
- This value is then XORed with the upper byte of `crc`, updating the CRC value for the current byte.
9. After processing all bytes in the input array, the function returns the final CRC value as a string by converting `crc` to a string using `ToString()`.
In summary, this function `Crc16Ccitt` calculates the 16-bit CRC using the CCITT polynomial for the input byte array. This type of CRC calculation is commonly used in various communication protocols and error-checking mechanisms.
Ulasan