# Java CRC4校验

## 算法设计

① 循环计算，计算次数为扩大后的数据长度
② 如果数据首位不是0，分别将数据，特征码，校验首位所用的数据右移一位。否则执行③
③ 将特征码，校验 首位所用数据右移一位
④ 待求数如果小于0x10，跳出循环

## 全部代码

public class main {
private static final  short poly = 0x13;         //1 0011
/**
* 计算余数
* @param num 待检测序列
* @return  余数
*/
public static short getCrc(short num){

short extendPoly = (short) (poly << 9) ;                  //10 0110 0000 0000
short data = (short) (num << 4);     //待发送数据左移四位 11 0101 1011 0000
short detectNum = 0x2000;                   //校验首位  10 0000 0000 0000

String stringData = Integer.toBinaryString(data);         //转字符串，获取长度
for (int i = 0; i < stringData.length(); i ++){
String stringa = Integer.toBinaryString(data);
String stringb = Integer.toBinaryString(extendPoly);
if (data < 0x10){
break;
}else{
if((data & detectNum) != 0){
data = (short) (extendPoly ^ data);
System.out.println(stringa);

extendPoly  = (short) (extendPoly >> 1);
System.out.println(stringb);

System.out.println("---------------------");
detectNum = (short) (detectNum >> 1);
}else{
detectNum = (short) (detectNum >> 1);
extendPoly  = (short) (extendPoly >> 1);
System.out.println(stringa);
System.out.println(stringb);
System.out.println("---------------------");
}
}

}
String stringRemainder =  Integer.toBinaryString(data);
System.out.println("余数为："+stringRemainder);
return data;
}

/**
* 合成发送序列
* @param data  带传送数据
* @param remainder  余数
* @return  检验序列FCS
*/
public static short getFCS(short data,short remainder){

short FCS = (short) ((data << 4) + remainder);             //11 0101 1011 1110
String stringFCS =  Integer.toBinaryString(FCS);
System.out.println("\nFCS序列为:"+stringFCS+"\n");
return FCS;
}

/**
* 检测接收到的序列
* @param receiveData  接收数据
*/
public static void checkFCS(short receiveData){
short extendPoly = (short) (poly << 9) ;                   //10 0110 0000 0000
short detectNum = 0x2000;                                  //校验首位  10 0000 0000 0000

String stringFCS = Integer.toBinaryString(receiveData);         //转字符串，获取长度
for (int i = 0; i < stringFCS.length(); i ++){
String stringa = Integer.toBinaryString(receiveData);
String stringb = Integer.toBinaryString(extendPoly);
if (receiveData < 0x10){
break;
}else{
if((receiveData & detectNum) != 0){
System.out.println(stringa);

extendPoly  = (short) (extendPoly >> 1);
System.out.println(stringb);

System.out.println("---------------------");

detectNum = (short) (detectNum >> 1);
}else{
detectNum = (short) (detectNum >> 1);
extendPoly  = (short) (extendPoly >> 1);
System.out.println(stringa);
System.out.println(stringb);
System.out.println("---------------------");
}
}
}
System.out.println("没有差错,接收帧");
else {
System.out.println("出现差错,丢弃帧");
}
}

public static void main(String[] args) {
short num = 859;
String binaryString = Integer.toBinaryString(num);
System.out.println("输入数据为: "+binaryString+"\n");
short remainder = getCrc(num);

short FCS = getFCS(num,remainder);
checkFCS(FCS);

System.out.println("\n测试,改变CRC最后一位");
short errRemainder = 0xf;              //测试,改变最后余数最后一位
short testFcs = getFCS(num,errRemainder);
checkFCS(testFcs);
}
}

## 小结

#### CRC校验码生成与数据校验源码程序(逐位运算法) (包括CRC-4,5,6,7,8,16,32)

2016-10-05 09:40:18

#### CRC4 循环冗余校验

2014年11月09日 474KB 下载

#### CRC校验原理及查表码表由来

2017-01-19 13:40:46

#### 循环冗余校验（CRC）算法入门引导

2012-08-19 12:42:34

#### 数据链路层帧传输的三个基本问题

2017-12-20 18:53:31

#### CRC校验和CRC各种算法

2016-05-27 11:22:50

#### java CRC7校验

2007-12-18 16:36:00

#### JAVA 实现crc8（多项式X8+x5+x4+1）,byte的各种问题

2018-01-10 20:51:01

#### java来实现CRC校验

2014-07-25 12:54:00

#### <em>crc</em>l4<em>java</em>-base-1.2.jar

2018年06月26日 00:00