求助:谁有CRC16非查表的C语言的源码啊?

NJZD 2005-06-08 02:57:45
CRC16源码
...全文
124 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
NJZD 2005-06-09
  • 打赏
  • 举报
回复
为什么查表和非查表的结果高低字节反了,哪一个是正确定的啊!
dophin1003 2005-06-09
  • 打赏
  • 举报
回复
应该算法是一样的吧?
把1021改成8005就行了
NJZD 2005-06-09
  • 打赏
  • 举报
回复
我要的是X16+X15+X2+1
你们给我的好像的是CCITT的
foochow 2005-06-08
  • 打赏
  • 举报
回复
#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)
{
uint nRemainder, nRet;
int n;
uchar index;
ulong *pulTable = g_ulTable;

nRemainder = 0x0000;
for(n = 0; n < nBytes; n ++)
{
index = (uchar)CRCBitReflect(*(pucData + n), 8) ^ (nRemainder >> 8);
nRemainder = (uint)*(pulTable + index) ^ (nRemainder << 8);
}
nRet = (uint)CRCBitReflect(nRemainder, 16) ^ 0x0000;
return(nRet);
}

// 反转数据的比特位, 反转后MSB为1.
// 反转前: 1110100011101110 0010100111100000
// 反转后: 1111001010001110 1110001011100000
ulong _far CRCBitReflect(ulong ulData, int nBits)
{
ulong ulResult = 0x00000000L;
int n;

for(n = 0; n < nBits; n ++)
{
if(ulData & 0x00000001L)
{
ulResult |= (ulong)(1L << ((nBits - 1) - n));
}
ulData = (ulData >> 1);
}
return(ulResult);
}
「已注销」 2005-06-08
  • 打赏
  • 举报
回复
http://bbs.chinaunix.net/forum/23/20040108/239630.html
http://www.laogu.com/shownews.aspx?id=436
-------------
「已注销」 2005-06-08
  • 打赏
  • 举报
回复
http://www.dancefires.com/club/printpage.asp?BoardID=25&ID=326

69,373

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧