//************
//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] != ‘’)
      {
          unsigned int abc;
          abc = data[j];          
          cout<<hex<<((abc & 0xf0)>>4)<<(abc & 0xf);//输出ASCII。十进制数
                         //调用”hex”,输出十六进制数
          j++;
      }
      cout<<(CRC>>4)<<(CRC & 0xf)<<endl;

}

/*
为了不至于大家太过迷信他人的程序,本人没有使此源程序更完善。请各位积极思考^_-
*/