package test;
public class test1 {
public static int[] crc(int[] data) {
int[] temdata = new int[data.length + 2];
// unsigned char alen = *aStr – 2; //CRC16只计算前两部分
int xda, xdapoly;
int i, j, xdabit;
xda = 0xFFFF;
xdapoly = 0xA001; // (X**16 + X**15 + X**2 + 1)
for (i = 0; i < data.length; i++) {
xda ^= data[i];
for (j = 0; j < 8; j++) {
xdabit = (int) (xda & 0x01);
xda >>= 1;
if (xdabit == 1)
xda ^= xdapoly;
}
}
System.arraycopy(data, 0, temdata, 0, data.length);
temdata[temdata.length - 2] = (int) (xda & 0xFF);
temdata[temdata.length - 1] = (int) (xda >> 8);
return temdata;
}
public static void main(String[] args) {
int[] data={1,1};
int[] d1=crc(data);
for(int i=0;i<d1.length;i++){
System.out.print(d1[i]+" ");
}
System.out.println();
for(int i=0;i<d1.length;i++){
System.out.print(Integer.toHexString(d1[i])+" ");
}
}
}