CRC16算法系列文章:
前言
JDK里包含了CRC32的算法,但是没有CRC16的,网上搜了一堆没有找到想要的,索性自己实现
注意:CRC16算法分为很多种,本篇文章中,只讲其中的一种:CRC16-CCITT-FALSE算法
CRC16算法系列之一:CRC16-CCITT-FALSE算法的java实现
功能
1、支持short类型
2、支持int类型
3、支持数组任意区域计算
实现
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
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);
-
-
CRC16算法之一:CRC16-CCITT-FALSE算法的java实现
原文:https://www.cnblogs.com/eguid/p/9667137.html