C++语言CRC16校验算法:
----------------------------------------------------------
#include<iostream.h>
class CRC16
{
public :
int crc16 (unsigned char * data,int size)
{
unsigned char data_t ;
int crc,i,j;
crc = i = j = 0;
if (data == NULL)
{
return 0;
}
for (j=0;j<size;j++)
{
data_t = data [j];
crc = (data_t ^ crc);
for (i=0;i<8;i++)
{
if ( (crc&0x1)==1)
{
crc = (crc >> 1) ^ 0xA001;
}
else
{
crc >>= 1;
}
}
}
return crc;
}
};
int main ()
{
unsigned char array [] = {0x88,0x77,0xFF,0x9A};
int checkValue;
CRC16 test;
checkValue = test.crc16 (array,sizeof (array));
cout<<"CRC16 值 :"<<hex<< ( (checkValue >> 8) & 0xff);
cout<<" "<<hex<< (checkValue & 0xff)<<endl<<endl;
return 0;
}
------------------------------------------------------------------------