#include "stdio.h"
typedef unsigned char byte; //定义一个无符号1字节
typedef char cbyte; //定义一个有符号1字节
typedef unsigned char boolean; //定义一个bool类型
typedef unsigned short uword; //定义一个无符号2字节
typedef short word; //定义一个有符号2字节
uword crc16(byte* Buff, byte nSize)
{
uword m_Crc;
uword m_InitCrc = 0xffff;
uword i,j;
for(i=0; i<nSize; i++)
{
m_InitCrc ^= Buff[i];
for(j=0; j<8; j++)
{
m_Crc = m_InitCrc;
m_InitCrc >>= 1;
if(m_Crc&0x0001)
m_InitCrc ^= 0xa001;
}
}
return m_InitCrc; //返回 CRC16数值
}
void main()
{
byte buf[6]={0x01,0x03,0x10,0x33,0x00,0x03};
uword xor;
xor=crc16(buf,6);
printf("xor=%02x,xor1=%02x,xor2=%02x",xor,xor%256,xor/256);
}