crc校驗演算法java
CRC校驗碼的基本思想是利用線性編碼理論,在發送端根據要傳送的k位二進制碼序列,以一定的規則產生一個校驗用的監督碼(既CRC碼)r位,並附在信息後邊,構成一個新的二進制碼序列數共(k+r)位,最後發送出去。在接收端,則根據信息碼和CRC碼之間所遵循的規則進行檢驗,以確定傳送中是否出錯。
在數據存儲和數據通訊領域,CRC無處不在:著名的通訊協議X.25的FCS(幀檢錯序列)採用的是CRC. CCITT,ARJ、LHA等壓縮工具軟體採用的是CRC32,磁碟驅動器的讀寫採用了CRC16,通用的圖像存儲格式GIF、TIFF等也都用CRC作為檢錯手段。
CRC的本質是模-2除法的余數,採用的除數不同,CRC的類型也就不一樣。通常,CRC的除數用生成多項式來表示。最常用的CRC碼的生成多項式有CRC16,CRC32.
以CRC16為例,16位的CRC碼產生的規則是先將要發送的二進制序列數左移16位(既乘以2^16)後,再除以一個多項式,最後所得到的余數既是CRC碼,如下式所示,其中K(X)表示n位的二進制序列數,G(X)為多項式,Q(X)為整數,R(X)是余數(既CRC碼)。
K(X)>>16=G(x)Q(x)+R(x)
求CRC碼所採用模2加減運演算法則,既是不帶進位和借位的按位加減,這種加減運算實際上就是邏輯上的異或運算,加法和減法等價,乘法和除法運算與普通代數式的乘除法運算是一樣,符合同樣的規律。生成CRC碼的多項式如下,其中CRC-16和CRC-CCITT產生16位的CRC碼,而CRC-32則產生的是32位的CRC碼
接收方將接收到的二進制序列數(包括信息碼和CRC碼)除以多項式,如果余數為0,則說明傳輸中無錯誤發生,否則說明傳輸有誤,關於其原理這里不再多述。用軟體計算CRC碼時,接收方可以將接收到的信息碼求CRC碼,比較結果和接收到的CRC碼是否相同。
CCITT推薦的高級數據鏈路控制規程HDLC的幀校驗序列FCS中,使用CCITT-16即CRC16,其生成多項式為G(x)=x16+x12+x5+1,CRC-32的生成多項式為G(x)=x32+x26+x23+x22+x16+x11+x10+x16+x8+x7+x5+x4+x2+x+1
2. crc校驗求java到啊
public class CRC16 { public static String crc16(String src,int len){
int crc = 0x0000FFFF;
short tc;
char sbit;
for(int i=0;i<len; i++){
tc = (short)(crc >>>8);
crc = ((tc ^ src.charAt(i)) & 0x00FF); for(int r=0;r<8;r++){
sbit = (char)(crc & 0x01);
crc >>>= 1; //�1�7�1�7�1�7�0�6�1�7�1�7�1�7�1�7�1�7�1�7�1�7�0�5λ
if(sbit != 0)crc =(crc^0xA001) & 0x0000FFFF; }
}
//Integer.toHexString(crc);
return Integer.toHexString(crc);
}
}
3. 求java的 crc8演算法方法
你的意思就翻譯一下,是嗎:
static char crc8fun ( char in, char prest)
{
int loop;
char out;
char crc_pol=0xb8; /*多項式*/
out = in^prest;
for(loop=0;loop<8;loop++){
if(out&0x01){
out=(out>>1)^crc_pol;
}else{
out=(out>>1);
}
return out;
}
}
4. CRC校驗代碼 c++ java都可以
不會,建議還是自己做吧。
5. 如何用java實現CRC8驗證演算法
/*http://www.koders.com/java/.aspx?s=Address#L34
*---------------------------------------------------------------------------
* Copyright (C) 1999,2000 Dallas Semiconctor Corporation, All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation
* the rights to use, , modify, merge, publish, distribute, sublicense,
* and/or sell copies of the Software, and to permit persons to whom the
* Software is furnished to do so, subject to the following conditions:
*
* The above right notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL DALLAS SEMICONDUCTOR BE LIABLE FOR ANY CLAIM, DAMAGES
* OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
* ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
* OTHER DEALINGS IN THE SOFTWARE.
*
* Except as contained in this notice, the name of Dallas Semiconctor
* shall not be used except as stated in the Dallas Semiconctor
* Branding Policy.
*---------------------------------------------------------------------------
*/
package com.dalsemi.onewire.utils;
/**
* CRC8 is a class to contain an implementation of the
* Cyclic-Rendency-Check CRC8 for the iButton. The CRC8 is used
* in the 1-Wire Network address of all iButtons and 1-Wire
* devices.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @version 0.00, 28 Aug 2000
* @author DS
*
*/
public class CRC8
{
//--------
//-------- Variables
//--------
/**
* CRC 8 lookup table
*/
private static byte dscrc_table [];
/*
* Create the lookup table
*/
static
{
//Translated from the assembly code in iButton Standards, page 129.
dscrc_table = new byte [256];
int acc;
int crc;
for (int i = 0; i < 256; i++)
{
acc = i;
crc = 0;
for (int j = 0; j < 8; j++)
{
if (((acc ^ crc) & 0x01) == 0x01)
{
crc = ((crc ^ 0x18) >> 1) | 0x80;
}
else
crc = crc >> 1;
acc = acc >> 1;
}
dscrc_table [i] = ( byte ) crc;
}
}
//--------
//-------- Constructor
//--------
/**
* Private constructor to prevent instantiation.
*/
private CRC8 ()
{
}
//--------
//-------- Methods
//--------
/**
* Perform the CRC8 on the data element based on the provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc data element on which to perform the CRC8
* @param seed seed the CRC8 with this value
* @return CRC8 value
*/
public static int compute (int dataToCRC, int seed)
{
return (dscrc_table [(seed ^ dataToCRC) & 0x0FF] & 0x0FF);
}
/**
* Perform the CRC8 on the data element based on a zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc data element on which to perform the CRC8
* @return CRC8 value
*/
public static int compute (int dataToCRC)
{
return (dscrc_table [dataToCRC & 0x0FF] & 0x0FF);
}
/**
* Perform the CRC8 on an array of data elements based on a
* zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [])
{
return compute(dataToCrc, 0, dataToCrc.length);
}
/**
* Perform the CRC8 on an array of data elements based on a
* zero seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param off offset into array
* @param len length of data to crc
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int off, int len)
{
return compute(dataToCrc, off, len, 0);
}
/**
* Perform the CRC8 on an array of data elements based on the
* provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param off offset into array
* @param len length of data to crc
* @param seed seed to use for CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int off, int len, int seed)
{
// loop to do the crc on each data element
int CRC8 = seed;
for (int i = 0; i < len; i++)
CRC8 = dscrc_table [(CRC8 ^ dataToCrc [i + off]) & 0x0FF];
return (CRC8 & 0x0FF);
}
/**
* Perform the CRC8 on an array of data elements based on the
* provided seed.
* <p>
* CRC8 is based on the polynomial = X^8 + X^5 + X^4 + 1.
*
* @param dataToCrc array of data elements on which to perform the CRC8
* @param seed seed to use for CRC8
* @return CRC8 value
*/
public static int compute (byte dataToCrc [], int seed)
{
return compute(dataToCrc, 0, dataToCrc.length, seed);
}
}
6. java 實現crc16校驗演算法的問題
貌似CRC演算法循環求余時減法是不帶借位的。
7. 求助,一段C的CRC校驗演算法,需要翻譯成JAVA,感謝
publicstaticshortCRC16_WifiAllBtye(Numberdata,shortlen){
intcksum=0;
inttmp_32=0;
shorttmp_8=0;
shortp=data.shortValue();
if(data!=null){
while(len>1){
tmp_8=p++;
tmp_32=(int)(p++);
cksum+=tmp_32<<8;
cksum+=(int)tmp_8;
len-=2;
}
if(len>=0){
cksum+=(int)p;
}
cksum=(cksum>>16)+(cksum&0xFFFF);
cksum+=(cksum>>16);
}
return(short)(~cksum);
}
8. 用java實現一個CRC程序
強力圍觀,坐等高手回答!
9. 用java編寫一個獲得CRC校驗碼的javabean
private static String mkCrc16(String str) {
CRC16 crc16 = new CRC16();
byte[] b = str.getBytes();
for (int i = 0; i < b.length; i++)
crc16.update(b[i]);
return Integer.toHexString(crc16.value);
}
private static String mkCrc(String string) throws Exception {
CRC32 crc32 = new CRC32();
crc32.update(string.getBytes());
return Long.toHexString(crc32.getValue());
}
public class CRCUtil {
public static final int evalCRC16(byte[] data) {
int crc = 0xFFFF;
for (int i = 0; i < data.length; i++) {
crc = (data[i] << 8) ^ crc;
for (int j = 0; j < 8; ++j)
if ((crc & 0x8000) != 0)
crc = (crc << 1) ^ 0x1021;
else
crc <<= 1;
}
return (crc ^ 0xFFFF) & 0xFFFF;
}
}
10. JAVA怎麼做CRC校驗的程序
實現方法:最簡單的校驗就是把原始數據和待比較數據直接進行比較,看是否完全一樣這種方法是最安全最准確的。同時也是效率最低的。
應用例子:龍珠cpu在線調試工具bbug.exe。它和龍珠cpu間通訊時,bbug發送一個位元組cpu返回收到的位元組,bbug確認是剛才發送位元組後才繼續發送下一個位元組的。 實現方法:在數據存儲和傳輸中,位元組中額外增加一個比特位,用來檢驗錯誤。校驗位可以通過數據位異或計算出來。
應用例子:單片機串口通訊有一模式就是8位數據通訊,另加第9位用於放校驗值。
bcc異或校驗法(block check character)
實現方法:很多基於串口的通訊都用這種既簡單又相當准確的方法。它就是把所有數據都和一個指定的初始值(通常是0)異或一次,最後的結果就是校驗值,通常把它附在通訊數據的最後一起發送出去。接收方收到數據後自己也計算一次異或和校驗值,如果和收到的校驗值一致就說明收到的數據是完整的。
校驗值計算的代碼類似於:
unsigned uCRC=0;//校驗初始值
for(int i=0;i<DataLenth;i++) uCRC^=Data[i];
適用范圍:適用於大多數要求不高的數據通訊。
應用例子:ic卡介面通訊、很多單片機系統的串口通訊都使用。 (Cyclic Rendancy Check)
實現方法:這是利用除法及余數的原理來進行錯誤檢測的