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