public class crcMain {
//crc16 -1021型校验,
//CRC16-CCITT x16+x12+x5+1 1021 ISO HDLC, ITU X.25, V.34/V.41/V.42, PPP-FCS
public static int[] Crc16Calc(byte[] data_arr, int data_len)
{
int crc16 = 0;
int i;
for(i =0; i < (data_len); i++)
{
crc16 = (char)(( crc16 >> 8) | (crc16 << 8));
crc16 ^= data_arr[i]& 0xFF;
crc16 ^= (char)(( crc16 & 0xFF) >> 4);
crc16 ^= (char)(( crc16 << 8) << 4);
crc16 ^= (char)((( crc16 & 0xFF) << 4) << 1);
}
int [] result=new int[2];
result[0]= (crc16/256) ;
result[1]= (crc16%256) ;
return result;
}
public static void main(String[] args) {
byte[] aa = {0x02, 0x05, 0x00, 0x03, (byte) 0xff, 0x00,0x00,0x01 };
//
int[] result=Crc16Calc(aa,aa.length);
//System.out.println(new String(result));
System.out.println(String.format("0x%02x", result[0])+","+String.format("0x%02x", result[1]));
}
}