CRC16算法系列文章:java
前言
JDK裏包含了CRC32的算法,可是沒有CRC16的,網上搜了一堆沒有找到想要的,索性本身實現算法
注意:CRC16算法分爲不少種,本篇文章中,只講其中的一種:CRC16-CCITT-FALSE算法數組
CRC16算法系列之一:CRC16-CCITT-FALSE算法的java實現工具
功能
一、支持short類型ui
二、支持int類型加密
三、支持數組任意區域計算spa
實現
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
public static int crc16(byte[] bytes) {
-
return crc16(bytes, bytes.length);
-
-
-
-
-
-
-
-
-
public static int crc16(byte[] bytes, int len) {
-
-
for (int j = 0; j < len; j++) {
-
crc = ((crc >>>
8) | (crc << 8)) & 0xffff;
-
crc ^= (bytes[j] &
0xff);
-
crc ^= ((crc &
0xff) >> 4);
-
crc ^= (crc <<
12) & 0xffff;
-
crc ^= ((crc &
0xFF) << 5) & 0xffff;
-
-
-
-
-
-
-
-
-
-
-
-
public static int crc16(byte[] bytes, int start, int len) {
-
-
for (; start < len; start++) {
-
crc = ((crc >>>
8) | (crc << 8)) & 0xffff;
-
crc ^= (bytes[start] &
0xff);
-
crc ^= ((crc &
0xff) >> 4);
-
crc ^= (crc <<
12) & 0xffff;
-
crc ^= ((crc &
0xFF) << 5) & 0xffff;
-
-
-
-
-
-
-
-
-
-
-
-
-
public static short crc16_short(byte[] bytes) {
-
return crc16_short(bytes, 0, bytes.length);
-
-
-
-
-
-
-
-
-
-
-
-
public static short crc16_short(byte[] bytes, int len) {
-
return (short) crc16(bytes, len);
-
-
-
-
-
-
-
-
-
public static short crc16_short(byte[] bytes, int start, int len) {
-
return (short) crc16(bytes, start, len);
-
-