crc16的算法?

来源:百度知道 编辑:UC知道 时间:2024/06/07 09:59:23

#define CRC16_POLYNOMIAL 0x1021 // CRC_16校验方式的多项式.

typedef unsigned char uchar;
typedef unsigned int uint;
typedef unsigned long ulong;
typedef enum tagBoolean { FALSE, TRUE } bool;

ulong g_ulTable[256];

// CRC_16方式校验的初始化函数, 计算CRC_16余数表.
void _far CRC16Init(void)
{
uint nRemainder;
int n, m;
ulong *pulTable = g_ulTable;

for(n = 0; n < 256; n ++)
{
nRemainder = (uint)n << 8;
for(m = 8; m > 0; m --)
{
if(nRemainder & 0x8000)
{
nRemainder = (nRemainder << 1) ^ CRC16_POLYNOMIAL;
}
else
{
nRemainder = (nRemainder << 1);
}
}
*(pulTable + n) = nRemainder;
}
}

// 以CRC_16方式计算一个数据块的CRC值.
// pucData - 待校验的数据块指针.
// nBytes - 数据块大小, 单位是字节.
// 返回值是无符号的长整型, 其中低16位有效.
ulong _far CRC16Calc(uchar *pucData, int nBytes)
{
uin