|
- #include "stdafx.h"
- #include "iostream"
- typedef unsigned short ushort;
- typedef unsigned char uchar;
- using namespace std;
- ushort checksum(uchar* ,uchar ); //傳字符串,字節(jié)數(shù),
- int main()
- {
- uchar array[3]={0x01,0x02,0x03};
- uchar len=sizeof(array);
- ushort CRC=checksum(array ,len );
- cout<<hex<<CRC<<endl;
- return 0;
- }
- ushort checksum(uchar* array,uchar len)
- {
- ushort CRC=0xFFFF;
- for(uchar i=0;i<len;++i)
- {
- CRC=CRC^(ushort)array[i];
- for(uchar j=0;j<8;++j)
- {
-
- if((CRC & 0x0001)==0x0001)
- {
- CRC=(CRC>>=1)^0xA001;
- ;
- }
- else
- CRC>>=1;
- }
- }
- return CRC;
- }
復(fù)制代碼 |
|