/**////////////////////////////////////////////////////////////////////Discription:对数据生成CRC校验码;//Version: 1.0 2007-12.11//Modify: no//实现思想:// 1:定义生成表达式:G(x)=...(r阶)// 2:要得到校验码的帧:M(x)=...// 3:将帧左移r位;// 4:进行异或运算;/**////////////////////////////////////////////////////////////////////#include <stdio.h>/**//*功能:获取有效的二进制位数;参数:nData[in]: 要获取二进制位数的数据;返回:成功返回数据的二进制位数。说明:如果该数为0,则返回的二进制位数为0;修改记录:无------------------------------------------------------------------*/unsigned int GetBits(unsigned int nData)...{ unsigned int nNum=0; while (nData!=0) ...{ nData=nData>>1; nNum++; } return nNum;}/**//*功能:根据指定多项式得到指定数据的CRC校验码参数:nData[in]:要得到校验码的数据; nPoly[in]:预先设定的生成多项式;返回值:成功返回得到的CRC校验码;-----------------------------------------------------------------*/unsigned int GetCRC(unsigned int nData,unsigned int nPoly)...{ unsigned int nRank=GetBits(nPoly)-1;//得到多项式的阶.固定值。 unsigned int nbits; unsigned int nFront; unsigned int nBehind; unsigned int nMulData = nData<<nRank;//得到移动之后的数据; nbits = GetBits(nMulData);//得到数据的位数,会根据数据变动而变化。 /**//*当要分析的数据二进制位数小于(nRank+1)说明得到最终的校验码*/ while(nbits>nRank) ...{ nFront = nMulData>>(nbits-nRank-1); //初始参与计算的数据。此后该变量都是代表余数; nBehind =nMulData^(nFront<<(nbits-nRank-1));//得到后半部分待分析的数据; // printf("nMulData=%d,nFront=%d,nBehind=%d,nbits=%d ",nMulData,nFront,nBehind,nbits); nFront = nFront^nPoly;//得到余数; nMulData =(nFront<<(nbits-nRank-1))^nBehind; // printf("余数=%d,新nMulData=%d ",nFront,nMulData); nbits = GetBits(nMulData); } return nMulData;}int main(void)...{ printf("GetBits(216)=%d ",GetBits(216)); printf("GetCRC(216,4449)=%d ",GetCRC(216,69665)); return 0;}