java实现byte[ ] 的CRC16校验码
public class Crc16Util {
public static int CRC_XModem(byte[] bytes){
int crc = 0x00;
int polynomial = 0x1021;
for (int index = 0 ; index< bytes.length; index++) {
byte b = bytes[index];
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
return crc;
}
public static void main(String[] args) {
byte[] as={0x01 ,0x01, 0x02, 0x03, 0x04 ,0x05, 0x06, 0x07 ,0x08 ,0x09, 0x05, 0x06 ,0x07, 0x08};
System.out.println(CRC_XModem(as));
}
}
输出结果是:十进制数 45088