資源簡介
多項式編碼(polynomial code),也稱為CRC(cyclic redundancy check,循環冗余校驗碼),多項式編碼的思想是:將位串看成是系數為0或1的多項式。CRC校驗保護的單位是數據塊。數據塊的大小根據實際情況而定。每一個數據塊均被看作是一個二進制多項式,即所有系數均為二進制(即1或0)的多項式。
代碼片段和文件信息
package?www.zhiman.com;
import?java.util.Scanner;
import?java.math.*;
/**
*?@Description?TODO?計算循環冗余碼?需注意多項式位數與幀位數之和不得超過32位!
*?@author?zhiman?in?2017年8月28日?下午10:48:26?mail-zhimanma@gmail.com
*?@version?V1.0
*?
*/
public?class?CrcAlgorithm?{
public?static?void?main(String[]?args)?{
print(“請輸入二進制數據:?“);
Scanner?sc?=?new?Scanner(System.in);
String?dataStr?=?sc.next();
print(“請輸入多項式系數:?“);
String?gxStr?=?sc.next();
sc.close();
//獲取二進制幀的位數
int?dataStrLen?=?dataStr.length();
//獲取多項式位數
int?gxStrLen?=?gxStr.length();
//將二進制的字符串變為整型
int?data?=?toInt(dataStr);
//將多項式的字符串變為整型
int?gx?=?toInt(gxStr);
//算出原始數據補零后的總位數
int?sum?=?dataStrLen+gxStrLen-1;
//計算2的sum-1次冪
BigInteger?bi?=?new?BigInteger(“2“);
//將2的sum-1次冪轉換為int型
int?flag?=?bi.pow(sum-1).intValue();
//原始幀低位補零
data?=?data<<(
評論
共有 條評論