//************
//CRC-8部分源程序(C++实现)
//************
//Made By pcMingStudio
//转载请注明版权
//===========
#include<iostream>
#include<string>
using namespace std;
//————
//CRC-8算法
//由互联网中各种实现方法加以修改
unsigned int FindCRC(string &data,unsigned int datalen)
{
unsigned int CRC=0;
unsigned int genPoly = 0x07;
for (int i=0; i<datalen; i++)
{
CRC ^= data[i];
for(int j = 0; j<8; j++)
{
if(CRC & 0x80 )
CRC = (CRC << 1) ^ genPoly;
else
CRC <<= 1;
}
}
CRC &= 0xff;//保证CRC余码输出为2字节。
return CRC;
}
//========
//独力打造 ^_^
void main()
{
string data;
unsigned int datalen = 0;
cout << “enter data to be CRC: “;
cin >> data;
datalen = data.length();//计算字符串长度
cout<<“datalen “<<datalen<<endl;
unsigned int CRC;
CRC = FindCRC(data,datalen);
cout<<“CRC = “<<hex<<(CRC>>4)<<(CRC & 0xf)<<endl;
int j=0;
while(data[j] != ‘
近期评论