TA的每日心情 | 开心 2021-3-12 23:18 |
---|
签到天数: 2 天 [LV.1]初来乍到
|
document.body.oncopy = function () {
if (window.clipboardData) {
setTimeout(function () {
var text = clipboardData.getData("text");
if (text && text.length > 300) {
text = text + "
本文来自CSDN博客,转载请标明出处:" + location.href;
clipboardData.setData("text", text);
}
}, 100);
}
}
function StorePage() { d = document; t = d.selection ? (d.selection.type != "None" ? d.selection.createRange().text : "") : (d.getSelection ? d.getSelection() : ""); void (keyit = window.open("http://www.365key.com/storeit.aspx?t=" + escape(d.title) + "&u=" + escape(d.location.href) + "&c=" + escape(t), "keyit", "scrollbars=no,width=475,height=575,left=75,top=20,status=no,resizable=yes")); keyit.focus(); }
这两天项目中要使用到CRC校验功能,网上大量的例子是针对c、delphi的例子,前期没有做过,理论上也欠缺很多知识,在这里对java如何实现我们想要的crc校验功能做一下自己的总结,以下内容有本分转自网上,希望对有需要的朋友作能够参考一下。
一、什么是CRC校验:
CRC即循环冗余校验码(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定。
二、CRC的原理:若设码字长度为N,信息字段为K位,校验字段为R位(N=K+R),则对于CRC码集中的任一码字,存在且仅存在一个R次多项式g(x),使得
V(x)=A(x)g(x)=x[sup]R[/sup]m(x)+r(x);
其中: m(x)为K次信息多项式, r(x)为R-1次校验多项式,
g(x)称为生成多项式:
g(x)=g[sub]0[/sub]+g[sub]1[/sub]x+ g[sub]2[/sub]x[sup]2[/sup]+...+g[sub](R-1)[/sub]x[sup](R-1)[/sup]+g[sub]R[/sub]x[sup]R[/sup]
发送方通过指定的g(x)产生CRC码字,接收方则通过该g(x)来验证收到的CRC码字。
(对于细节性的知识朋友们可以在网上找到大量资料这里不再做重复说明) (以上来自网络转载)
三、CRC校验码方式有哪些:
1 byte checksum
CRC-16
CRC-16 (Modbus)
CRC-16 (Sick)
CRC-CCITT (XModem)
CRC-CCITT (0xFFFF)
CRC-CCITT (0x1D0F)
CRC-CCITT (Kermit)
CRC-DNP
CRC-32
对于以上方式来说结果可以生成为ASCII、HEX等方式
四、JAVA实现CRC校验
(1)这里对于CRC-CCITT四种方式进行梳理:
public class CRC {
/**
* CRC-CCITT(Kermit)验证模式
* @param str
* @return
*/
public String CRC_CCITT_Kermit(String str) {
int j, b, rrrc, c, i;
String tmpBalance;
int k;
rrrc = 0;
tmpBalance = str;
int tmpInt, CharInt;
String tmpChar, tmpStr;
tmpStr = "";
int High;
int Low;
for (j = 1; j <= 3; j++) {
if (Character.isDigit(tmpBalance.charAt(2 * j - 2))) {
High = Integer.parseInt(tmpBalance.charAt(2 * j - 2) + "");
} else {
High = 0;
}
if (Character.isDigit(tmpBalance.charAt(2 * j - 1))) {
Low = Integer.parseInt(tmpBalance.charAt(2 * j - 1) + "");
} else {
Low = 0;
}
High = (High & 0xff) << 4;
High = High | Low;
k = High;
for (i = 1; i <= 8; i++) {
c = rrrc & 1;
rrrc = rrrc >> 1;
if ((k & 1) != 0) {
rrrc = rrrc | 0x8000;
}
if (c != 0) {
rrrc = rrrc ^ 0x8408;
}
k = k >> 1;
}
}
for (i = 1; i <= 16; i++) {
c = rrrc & 1;
rrrc = rrrc >> 1;
if (c != 0) {
rrrc = rrrc ^ 0x8408;
}
}
c = rrrc >> 8;
b = rrrc << 8;
rrrc = c | b;
tmpInt = rrrc;
tmpStr = "";
for (i = 1; i <= 4; i++) {
tmpChar = "";
CharInt = tmpInt % 16;
if (CharInt > 9) {
switch (CharInt) {
case 10:
tmpChar = "A";
break;
case 11:
tmpChar = "B";
break;
case 12:
tmpChar = "C";
break;
case 13:
tmpChar = "D";
break;
case 14:
tmpChar = "E";
break;
case 15:
tmpChar = "F";
break;
}
} else {
tmpChar = Integer.toString(CharInt);
}
tmpInt = tmpInt / 16;
tmpStr = tmpChar + tmpStr;
}
System.out.println("tmpStr:" + tmpStr);
return tmpStr;
}
/**
* CRC-CCITT(XModem)
* CRC-CCITT(0xFFFF)
* CRC-CCITT(0x1D0F)
* 校验模式
* @param flag< XModem(flag=1) 0xFFFF(flag=2) 0x1D0F(flag=3)>
* @param str
* @return
*/
public String CRC_CCITT( int flag,String str) {
int crc = 0x00; // initial value
int polynomial = 0x1021;
byte[] bytes=str.getBytes();
switch(flag){
case 1:
crc=0x00;
break;
case 2:
crc=0xFFFF;
break;
case 3:
crc=0x1D0F;
break;
}
for (int index = 0 ; index< bytes.length; index++) {
byte b = bytes[index];
for (int i = 0; i < 8; i++) {
boolean bit = ((b >> (7-i) & 1) == 1);
boolean c15 = ((crc >> 15 & 1) == 1);
crc <<= 1;
if (c15 ^ bit) crc ^= polynomial;
}
}
crc &= 0xffff;
str = Integer.toHexString(crc);
return str;
}
源码下载:http://file.javaxxz.com/2014/11/1/000000500.zip |
|