今天需要用到CRC16校验,网上找了很多,但都没有解决我的问题,最后自己解决了,输出是CB 13,在CRC16校验软件上验证过。
public class main {
public static void main(String[] args){
Byte[] bb = new Byte[]{(byte) 0xC0,0x01 ,0x01 ,0x01 ,0x01 ,0x01 ,0x02};
List<Byte> list = new ArrayList<Byte>(Arrays.asList(bb));
short c =crc16_data2(list,7,(short) 0);
byte l= (byte) (c&0xFF);
byte h = (byte) (c>>8);
System.out.println(l);
System.out.println(h);
}
public static short crc16_data2(List<Byte> data, int len, short acc)
{
int ii = 0;
short crc = 0;
int tlen = len;
crc = acc;
int i=0;
while (tlen>0)
{
crc = (short) (crc ^ (short)((data.get(i) << 8) & 0xFFFF));
ii = 8;
while(ii>0)
{
if ((crc & 0x8000) != 0) crc = (short) (crc << 1 ^ 0x1021);
else crc = (short) (crc << 1);
ii--;
};
tlen --;
i++;
}
return crc;
}