赫赫,不限手法,只要结果正确。
struct B
{
unsigned char lo;
unsigned char hi;
};
union w2b
{
unsigned short w;
struct B b;
};
union s2b
{
signed short w;
struct B b;
};
unsigned short CRC16(unsigned char *buf, unsigned char len)
{
union w2b CRC16;
unsigned char i,j;
unsigned short poly = 0xa001;
CRC16.w = 0xffff;
while(len--)
{
CRC16.b.lo ^= *buf++;
for (i = 0; i < 8; i++)
{
j = CRC16.b.lo & 0x01;
CRC16.w >>= 1;
if (j) CRC16.w ^= poly;
};
};
return CRC16.w;
}