unsigned short CRC16(void * Buff_addr,unsigned short len)
{
unsigned char * dataPtr=(unsigned char *)Buff_addr;
unsigned short index=0;
unsigned short crc=0;
while(len--)
{
crc=(unsigned char)(crc>>8)|(crc<<8);
crc^=dataPtr[index++];
crc^=(unsigned char)(crc&0xff)>>4;
crc^=(crc<<8)<<4;
crc^=((crc&0xff)<<4)<<1;
}
return crc;
}
最好可以有个简单测试可以在eclipse下面运行的~
------解决方案--------------------
试试这个,用c和java跑出来结果一样
java里short最大值32767,只能用int了。
public class Test01 {
public static void main(String[] args) {
int crc = CRC16(new byte[] { 0x01, 0x02, 0x03, 0x04, 0x03, 0x02 }, 6);
System.out.println(Integer.toHexString(crc));
}
static int CRC16(byte[] buffer, int len) {
int index = 0;
int crc = 0;
while (len-- > 0) {
crc = (crc >> 8)
------解决方案--------------------
(crc << 8);
crc ^= buffer[index++];
crc ^= (crc & 0xff) >> 4;
crc ^= (crc << 8) << 4;
crc ^= ((crc & 0xff) << 4) << 1;
crc &= 0xFFFF;
}
return crc;
}
}