private static final int POLYNOMIAL = 0x1021; // CRC-16-XMODEM 标准多项式
private static final int INITIAL = 0x0000; // 初始值
/**
* CRC-16-XMODEM校验
* @param bytes 待校验的值
* @return crc校验码
*/
public static int calculateCRC16XMODEM(byte[] bytes) {
int crc = INITIAL;
for (byte b : bytes) {
crc ^= (b & 0xFF) << 8; // XOR with byte value
for (int i = 0; i < 8; i++) {
if ((crc & 0x8000) != 0) {
crc = (crc << 1) ^ POLYNOMIAL;
} else {
crc <<= 1;
}
}
}
return crc & 0xFFFF;
}
12-01
3910
