/**
* CRC-16
*
*
*
*
名称*
多项式*
初始值*
异或值*
Bit反转*
*
*
CRC-16/IBM*
0x8005*
0x0000*
0x0000*
LSB First*
*
*
CRC-16/CCITT*
0x1021*
0x0000*
0x0000*
LSB First*
*
*
CRC-16/CCITT-FALSE*
0x1021*
0xFFFF*
0x0000*
MSB First*
*
*
CRC-16/DECT R*
0x0589*
0x0000*
0x0001*
MSB First*
*
*
CRC-16/DECT X*
0x0589*
0x0000*
0x0000*
MSB First*
*
*
CRC-16/DNP*
0x3D65*
0x0000*
0xFFFF*
LSB First*
*
*
CRC-16/GENIBUS*
0x1021*
0xFFFF*
0xFFFF*
MSB First*
*
*
CRC-16/MAXIM*
0x8005*
0x0000*
0xFFFF*
LSB First*
*
*
CRC-16/MODBUS*
0x8005*
0xFFFF*
0x0000*
LSB First*
*
*
CRC-16/USB*
0x8005*
0xFFFF*
0xFFFF*
LSB First*
*
*
CRC-16/X25*
0x1021*
0xFFFF*
0xFFFF*
LSB First*
*
*
CRC-16/XMODEM*
0x1021*
0x0000*
0x0000*
MSB First*
*
*
* @author unnamed
*
*/
public class CRC16Utils {
/**
* CRC-16/IBM
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x8005*
0x0000*
0x0000*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_IBM(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
// Integer.reverse(0x8005) >>> 16
int wCPoly = 0xA001;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x0000;
}
/**
* CRC-16/CCITT
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x1021*
0x0000*
0x0000*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_CCITT(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
// Integer.reverse(0x1021) >>> 16
int wCPoly = 0x8408;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x0000;
}
/**
* CRC-16/CCITT-FALSE
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x1021*
0xFFFF*
0x0000*
MSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_CCITT_FALSE(byte[] source, int offset, int length) {
int wCRCin = 0xFFFF;
int wCPoly = 0x1021;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFFFF;
return wCRCin ^= 0x0000;
}
/**
* CRC-16/DECT R
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x0589*
0x0000*
0x0001*
MSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_DECT_R(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
int wCPoly = 0x0589;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFFFF;
return wCRCin ^= 0x0001;
}
/**
* CRC-16/DECT X
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x0589*
0x0000*
0x0000*
MSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_DECT_X(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
int wCPoly = 0x0589;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFFFF;
return wCRCin ^= 0x0000;
}
/**
* CRC-16/DNP
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x3D65*
0x0000*
0xFFFF*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_DNP(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
// Integer.reverse(0x3D65) >>> 16
int wCPoly = 0xA6BC;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0xFFFF;
}
/**
* CRC-16/GENIBUS
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x1021*
0xFFFF*
0xFFFF*
MSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_GENIBUS(byte[] source, int offset, int length) {
int wCRCin = 0xFFFF;
int wCPoly = 0x1021;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFFFF;
return wCRCin ^= 0xFFFF;
}
/**
* CRC-16/MAXIM
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x8005*
0x0000*
0xFFFF*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_MAXIM(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
// Integer.reverse(0x8005) >>> 16
int wCPoly = 0xA001;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0xFFFF;
}
/**
* CRC-16/MODBUS
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x8005*
0xFFFF*
0x0000*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_MODBUS(byte[] source, int offset, int length) {
int wCRCin = 0xFFFF;
// Integer.reverse(0x8005) >>> 16
int wCPoly = 0xA001;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0x0000;
}
/**
* CRC-16/USB
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x8005*
0xFFFF*
0xFFFF*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_USB(byte[] source, int offset, int length) {
int wCRCin = 0xFFFF;
// Integer.reverse(0x8005) >>> 16
int wCPoly = 0xA001;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0xFFFF;
}
/**
* CRC-16/X25
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x1021*
0xFFFF*
0xFFFF*
LSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_X25(byte[] source, int offset, int length) {
int wCRCin = 0xFFFF;
// Integer.reverse(0x1021) >>> 16
int wCPoly = 0x8408;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
wCRCin ^= ((int) source[i] & 0x00FF);
for (int j = 0; j < 8; j++) {
if ((wCRCin & 0x0001) != 0) {
wCRCin >>= 1;
wCRCin ^= wCPoly;
} else {
wCRCin >>= 1;
}
}
}
return wCRCin ^= 0xFFFF;
}
/**
* CRC-16/XMODEM
*
*
*
*
多项式*
初始值*
异或值*
Bit反转*
*
*
0x1021*
0x0000*
0x0000*
MSB First*
*
*
* @param source
* @param offset
* @param length
* @return
*/
public static int CRC16_XMODEM(byte[] source, int offset, int length) {
int wCRCin = 0x0000;
int wCPoly = 0x1021;
for (int i = offset, cnt = offset + length; i < cnt; i++) {
for (int j = 0; j < 8; j++) {
boolean bit = ((source[i] >> (7 - j) & 1) == 1);
boolean c15 = ((wCRCin >> 15 & 1) == 1);
wCRCin <<= 1;
if (c15 ^ bit)
wCRCin ^= wCPoly;
}
}
wCRCin &= 0xFFFF;
return wCRCin ^= 0x0000;
}
}