CRC(循环冗余校验)在线计算:
http://www.ip33.com/crc.html
LRC校验(纵向冗余校验)在线计算:
http://www.ip33.com/lrc.html
BCC校验(异或校验)在线计算:
http://www.ip33.com/bcc.html
CRC校验(循环冗余校验)小知识
CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种查错校验码,其特征是信息字段和校验字段的长度可以任意选定。循环冗余检查(CRC)是一种数据传输检错功能,对数据进行多项式计算,并将得到的结果附在帧的后面,接收设备也执行类似的算法,以保证数据传输的正确性和完整性。
- CRC算法参数模型解释:
NAME:参数模型名称。
WIDTH:宽度,即CRC比特数。
POLY:生成项的简写,以16进制表示。例如:CRC-32即是0x04C11DB7,忽略了最高位的"1",即完整的生成项是0x104C11DB7。
INIT:这是算法开始时寄存器(crc)的初始化预置值,十六进制表示。
REFIN:待测数据的每个字节是否按位反转,True或False。
REFOUT:在计算后之后,异或输出之前,整个数据是否按位反转,True或False。
XOROUT:计算结果与此参数异或后得到最终的CRC值。
常见CRC参数模型如下:
CRC算法名称 | 多项式公式 | 宽度 | 多项式 | 初始值 | 结果异或值 | 输入反转 | 输出反转 |
---|---|---|---|---|---|---|---|
CRC-4/ITU | x4 + x + 1 | 4 | 03 | 00 | 00 | true | true |
CRC-5/EPC | x5 + x3 + 1 | 5 | 09 | 09 | 00 | false | false |
CRC-5/ITU | x5 + x4 + x2 + 1 | 5 | 15 | 00 | 00 | true | true |
CRC-5/USB | x5 + x2 + 1 | 5 | 05 | 1F | 1F | true | true |
CRC-6/ITU | x6 + x + 1 | 6 | 03 | 00 | 00 | true | true |
CRC-7/MMC | x7 + x3 + 1 | 7 | 09 | 00 | 00 | false | false |
CRC-8 | x8 + x2 + x + 1 | 8 | 07 | 00 | 00 | false | false |
CRC-8/ITU | x8 + x2 + x + 1 | 8 | 07 | 00 | 55 | false | false |
CRC-8/ROHC | x8 + x2 + x + 1 | 8 | 07 | FF | 00 | true | true |
CRC-8/MAXIM | x8 + x5 + x4 + 1 | 8 | 31 | 00 | 00 | true | true |
CRC-16/IBM | x16 + x15 + x2 + 1 | 16 | 8005 | 0000 | 0000 | true | true |
CRC-16/MAXIM | x16 + x15 + x2 + 1 | 16 | 8005 | 0000 | FFFF | true | true |
CRC-16/USB | x16 + x15 + x2 + 1 | 16 | 8005 | FFFF | FFFF | true | true |
CRC-16/MODBUS | x16 + x15 + x2 + 1 | 16 | 8005 | FFFF | 0000 | true | true |
CRC-16/CCITT | x16 + x12 + x5 + 1 | 16 | 1021 | 0000 | 0000 | true | true |
CRC-16/CCITT-FALSE | x16 + x12 + x5 + 1 | 16 | 1021 | FFFF | 0000 | false | false |
CRC-16/X25 | x16 + x12 + x5 + 1 | 16 | 1021 | FFFF | FFFF | true | true |
CRC-16/XMODEM | x16 + x12 + x5 + 1 | 16 | 1021 | 0000 | 0000 | false | false |
CRC-16/DNP | x16 + x13 + x12 + x11 + x10 + x8 + x6 + x5 + x2 + 1 | 16 | 3D65 | 0000 | FFFF | true | true |
CRC-32 | x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 | 32 | 04C11DB7 | FFFFFFFF | FFFFFFFF | true | true |
CRC-32/MPEG-2 | x32 + x26 + x23 + x22 + x16 + x12 + x11 + x10 + x8 + x7 + x5 + x4 + x2 + x + 1 | 32 | 04C11DB7 | FFFFFFFF | 00000000 | false | false |
案例一:
指令说明
指令
案例二:
指令说明
指令
CRC8
使用列子
byte[] data = new byte[8];
data[0] = (byte) 0xA5;
data[1] = (byte) 0x01;
data[2] = (byte) ~0x01;
data[3] = (byte) 0x00;
data[4] = (byte) seqNo;
data[5] = (byte) 0;
data[6] = (byte) 0;
data[7] = BleCRC.calCRC8(data);
public class BleCRC {
/**
* CRC8 code table
*/
private static final char[] Table_CRC8 = { 0x00, 0x07, 0x0E, 0x09, 0x1C,
0x1B, 0x12, 0x15, 0x38, 0x3F, 0x36, 0x31, 0x24, 0x23, 0x2A, 0x2D,
0x70, 0x77, 0x7E, 0x79, 0x6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0x7F, 0x6A, 0x6D, 0x64, 0x63, 0x3E, 0x39, 0x30, 0x37, 0x22, 0x25,
0x2C, 0x2B, 0x06, 0x01, 0x08, 0x0F, 0x1A, 0x1D, 0x14, 0x13, 0xAE,
0xA9, 0xA0, 0xA7, 0xB2, 0xB5, 0xBC, 0xBB, 0x96, 0x91, 0x98, 0x9F,
0x8A, 0x8D, 0x84, 0x83, 0xDE, 0xD9, 0xD0, 0xD7, 0xC2, 0xC5, 0xCC,
0xCB, 0xE6, 0xE1, 0xE8, 0xEF, 0xFA, 0xFD, 0xF4, 0xF3 };
/**
* Generate CRC8 code
* @param buf Data buffer
* @return CRC8 code, return 0 when the parameter is error
*/
public static byte calCRC8(byte[] buf) {
if (buf == null || buf.length == 0) {
return 0;
}
byte crc = 0;
for (int i = 0; i < buf.length-1; i++) {
crc = (byte)Table_CRC8[0x00ff & (crc ^ (buf[i]))];
}
return crc;
}
}
LRC
使用列子
byte[] data = new byte[8];
data[0] = (byte) 0xA5;
data[1] = (byte) 0x01;
data[2] = (byte) ~0x01;
data[3] = (byte) 0x00;
data[4] = (byte) seqNo;
data[5] = (byte) 0;
data[6] = (byte) 0;
data[7] = BleCRC.getLRC(data);
//或者
byte[] data = new byte[8];
data[0] = (byte) 0xA5;
data[1] = (byte) 0x01;
data[2] = (byte) ~0x01;
data[3] = (byte) 0x00;
data[4] = (byte) seqNo;
data[5] = (byte) 0;
data[6] = (byte) 0;
data[7] = (byte) (bytes[0] ^ bytes[1] ^ bytes[2] ^ bytes[3] ^ bytes[4] ^ bytes[5] ^ bytes[6] ^ bytes[7];
/*
* 输入byte[] data , 返回LRC校验byte
*/
public static byte getLRC(byte[] data) {
int tmp = 0;
for (int i = 0; i < data.length; i++) {
tmp = tmp + (byte) data[i];
}
tmp = ~tmp;
tmp = (tmp & (0xff));
tmp += 1;
return (byte) tmp;
}
CRC16
public class CRC16Util {
/**
* 将int转换成byte数组,低位在前,高位在后
* 改变高低位顺序只需调换数组序号
*/
private static byte[] intToBytes(int value) {
byte[] src = new byte[2];
src[1] = (byte) ((value >> 8) & 0xFF);
src[0] = (byte) (value & 0xFF);
return src;
}
/**
* 获取源数据和验证码的组合byte数组
*
* @param strings 可变长度的十六进制字符串
* @return
*/
public static byte[] appendCrc16(String... strings) {
byte[] data = new byte[]{};
for (int i = 0; i < strings.length; i++) {
int x = Integer.parseInt(strings[i], 16);
byte n = (byte) x;
byte[] buffer = new byte[data.length + 1];
byte[] aa = {n};
System.arraycopy(data, 0, buffer, 0, data.length);
System.arraycopy(aa, 0, buffer, data.length, aa.length);
data = buffer;
}
return appendCrc16(data);
}
/**
* 获取源数据和验证码的组合byte数组
*
* @param aa 字节数组
* @return
*/
public static byte[] appendCrc16(byte[] aa) {
byte[] bb = getCrc16(aa);
byte[] cc = new byte[aa.length + bb.length];
System.arraycopy(aa, 0, cc, 0, aa.length);
System.arraycopy(bb, 0, cc, aa.length, bb.length);
return cc;
}
/**
* 获取验证码byte数组,基于Modbus CRC16的校验算法
*/
public static byte[] getCrc16(byte[] arr_buff) {
int len = arr_buff.length;
// 预置 1 个 16 位的寄存器为十六进制FFFF, 称此寄存器为 CRC寄存器。
int crc = 0xFFFF;
int i, j;
for (i = 0; i < len; i++) {
// 把第一个 8 位二进制数据 与 16 位的 CRC寄存器的低 8 位相异或, 把结果放于 CRC寄存器
crc = ((crc & 0xFF00) | (crc & 0x00FF) ^ (arr_buff[i] & 0xFF));
for (j = 0; j < 8; j++) {
// 把 CRC 寄存器的内容右移一位( 朝低位)用 0 填补最高位, 并检查右移后的移出位
if ((crc & 0x0001) > 0) {
// 如果移出位为 1, CRC寄存器与多项式A001进行异或
crc = crc >> 1;
crc = crc ^ 0xA001;
} else
// 如果移出位为 0,再次右移一位
crc = crc >> 1;
}
}
return intToBytes(crc);
}
}
byte与String互转
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class BytesHexStrTranslate {
public static void main(String[] args) throws Exception {
byte[] bytes = "测试".getBytes("utf-8");
System.out.println("字节数组为:" + Arrays.toString(bytes));
System.out.println("方法一:" + bytesToHexFun1(bytes));
System.out.println("方法二:" + bytesToHexFun2(bytes));
System.out.println("方法三:" + bytesToHexFun3(bytes));
System.out.println("==================================");
String str = "e6b58be8af95";
System.out.println("转换后的字节数组:" + Arrays.toString(toBytes(str)));
System.out.println(new String(toBytes(str), "utf-8"));
}
/**
* 把十六进制字符串转成字符数组
* @param value
* @return
*/
public static List<String> stringToStrZu(String value) {
List<String> deviceValue = new ArrayList<>();
int count = value.length();
int index = 0;
int end = 2;
while (count > 0) {
deviceValue.add(value.substring(index, end));
index += 2;
end += 2;
count -= 2;
}
return deviceValue;
}
private static final char[] HEX_CHAR = {'0', '1', '2', '3', '4', '5',
'6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f'};
/**
* 方法一:
* byte[] to hex string
*
* @param bytes
* @return
*/
public static String bytesToHexFun1(byte[] bytes) {
// 一个byte为8位,可用两个十六进制位标识
char[] buf = new char[bytes.length * 2];
int a = 0;
int index = 0;
for(byte b : bytes) { // 使用除与取余进行转换
if(b < 0) {
a = 256 + b;
} else {
a = b;
}
buf[index++] = HEX_CHAR[a / 16];
buf[index++] = HEX_CHAR[a % 16];
}
return new String(buf);
}
/**
* 方法二:
* byte[] to hex string
*
* @param bytes
* @return
*/
public static String bytesToHexFun2(byte[] bytes) {
char[] buf = new char[bytes.length * 2];
int index = 0;
for(byte b : bytes) { // 利用位运算进行转换,可以看作方法一的变种
buf[index++] = HEX_CHAR[b >>> 4 & 0xf];
buf[index++] = HEX_CHAR[b & 0xf];
}
return new String(buf);
}
/**
* 方法三:
* byte[] to hex string
*
* @param bytes
* @return
*/
public static String bytesToHexFun3(byte[] bytes) {
StringBuilder buf = new StringBuilder(bytes.length * 2);
for(byte b : bytes) { // 使用String的format方法进行转换
buf.append(String.format("%02x", new Integer(b & 0xff)));
}
return buf.toString();
}
/**
* 将16进制字符串转换为byte[]
*
* @param str
* @return
*/
public static byte[] toBytes(String str) {
if(str == null || str.trim().equals("")) {
return new byte[0];
}
byte[] bytes = new byte[str.length() / 2];
for(int i = 0; i < str.length() / 2; i++) {
String subStr = str.substring(i * 2, i * 2 + 2);
bytes[i] = (byte) Integer.parseInt(subStr, 16);
}
return bytes;
}
}