CRC-CCITT算法如何写
CRC-CCITT算法怎么写
Consider the n data bits as the coefficients of a polynomial M(x) of degree n-1, associating the MSB of the zero-th octet
with xn-1 and the LSB of the last octet with x0. Define the generator polynomial, GH(x), and the inversion polynomial,
IH(x).
GH(x) = x16 + x12 + x5 + 1 (B.15)
IH(x) = x15 + x14 + x13 + ... + x2 + x + 1 (B.16)
The CRC polynomial, FH(x), is then computed from the formula:
FH(x) = (x16 M(x) mod GH(x)) + IH(x) (B.17)
modulo 2, i.e. in GF(2).
The coefficients of FH(x) are placed in the CRC field with the MSB of the zero-th octet of the CRC corresponding to
x15 and the LSB of the next octet of the CRC corresponding to x0. For the CRC-CCITT calculation the initial remainder
shall be 0x0000
以上是我所使用crc-ccitt协议的描述,请问该怎么写算法,别从网上复制了,我从网上找的都不对。
生成多项式:GH(x) = x16 + x12 + x5 + 1
反转多项式:IH(x) = x15 + x14 + x13 + ... + x2 + x + 1
CRC多项式:FH(x) = (x16 M(x) mod GH(x)) + IH(x)
这是关键信息,请问具体算法该怎么写,完全搞不懂啊
------解决方案--------------------
CRC16的C语言代码:
typedef unsigned short uint16_t;
typedef unsigned char BYTE;
uint16_t crc16_ccitt(BYTE *ucbuf, int iLen)
{
uint16_t crc = 0xFFFF; // initial value
uint16_t polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < iLen; ++j) {
for (int i = 0; i < 8; i++) {
char bit = ((ucbuf[j] >> (7-i) & 1) == 1);
char c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
return crc;
}
Consider the n data bits as the coefficients of a polynomial M(x) of degree n-1, associating the MSB of the zero-th octet
with xn-1 and the LSB of the last octet with x0. Define the generator polynomial, GH(x), and the inversion polynomial,
IH(x).
GH(x) = x16 + x12 + x5 + 1 (B.15)
IH(x) = x15 + x14 + x13 + ... + x2 + x + 1 (B.16)
The CRC polynomial, FH(x), is then computed from the formula:
FH(x) = (x16 M(x) mod GH(x)) + IH(x) (B.17)
modulo 2, i.e. in GF(2).
The coefficients of FH(x) are placed in the CRC field with the MSB of the zero-th octet of the CRC corresponding to
x15 and the LSB of the next octet of the CRC corresponding to x0. For the CRC-CCITT calculation the initial remainder
shall be 0x0000
以上是我所使用crc-ccitt协议的描述,请问该怎么写算法,别从网上复制了,我从网上找的都不对。
生成多项式:GH(x) = x16 + x12 + x5 + 1
反转多项式:IH(x) = x15 + x14 + x13 + ... + x2 + x + 1
CRC多项式:FH(x) = (x16 M(x) mod GH(x)) + IH(x)
这是关键信息,请问具体算法该怎么写,完全搞不懂啊
------解决方案--------------------
CRC16的C语言代码:
typedef unsigned short uint16_t;
typedef unsigned char BYTE;
uint16_t crc16_ccitt(BYTE *ucbuf, int iLen)
{
uint16_t crc = 0xFFFF; // initial value
uint16_t polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)
for (int j = 0; j < iLen; ++j) {
for (int i = 0; i < 8; i++) {
char bit = ((ucbuf[j] >> (7-i) & 1) == 1);
char c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
return crc;
}