crc16 java包_【Java】CRC16Utils(CRC-16 工具类)

/**

* 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;

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值