CRC-CCITT 算法问题

悠游一叶 2010-02-04 04:12:36
有一块 通信上要用到 CRC-CCITT算法, 我在网上找了n个算法算出的结果有好几种,请大伙帮忙看看, 哪个是对的 那个是错的。
还有我的校验跟对方的算法算出来总是不一样, 我只知道他们的一组数据算出来的结果:

数据: 0x47,0x01,0x11,0x05,0x01,0x23,0x45, 0x67,0x01,0x23,0x45,0x67,0x11,0x22, 0x33,0x44,0x00,0x02,0x40,0x09
算出来的校验值是: 0x1659
不知道他们是怎么算的,跟网上找的都不一样, 那位大哥手里有算法算出来是这个结果啊!
...全文
469 6 打赏 收藏 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
javaee_ssh 2012-06-13
  • 打赏
  • 举报
回复
楼主,找没找到答案你要说声啊。。。
liulovz 2011-01-10
  • 打赏
  • 举报
回复
请问下楼主问题解决了没?鄙人尚未找到可实行的方法,如果楼主有方法,可否指点一二??
一头头 2010-02-04
  • 打赏
  • 举报
回复
用这个

public class CRC {
//according file calculate the 16-bit CRC of a string using a byte-oriented tableless algorithm
public short CalCRC(byte[] s, int len, short crcval) {

int CRC = crcval;
int c;
int q;
int j = 0;
for(;len > 0;len--){

c = s[j++];
q = (CRC^c) & 017;
CRC = (CRC >> 4) ^ (q* 010201);
q = (CRC ^(c >> 4)) & 017;
CRC = (CRC >> 4) ^ (q * 010201);
}


return (short)CRC;
}
悠游一叶 2010-02-04
  • 打赏
  • 举报
回复
我测试算出的结果:
CRC_CCITT1==a2b2
do_crc======4425
Crc_ccitt===3441
cal_crc=====abf5
java========5d4d

越整越有点晕了, 那个才是正确的啊 ,大伙帮帮忙啊
悠游一叶 2010-02-04
  • 打赏
  • 举报
回复
算法二:
	public static short do_crc(byte[] message){
int len = message.length;
int i, j;
short crc_reg;

crc_reg = (short)( (message[0] << 8) + message[1]);
for (i = 0; i < len; i++){
if (i < len - 2)
for (j = 0; j <= 7; j++)
{
if ((short)crc_reg < 0)
crc_reg = (short)(((crc_reg << 1) + (message[i + 2] >> (7 - i))) ^ 0x1021);
else
crc_reg = (short)((crc_reg << 1) + (message[i + 2] >> (7 - i)));
}
else
for (j = 0; j <= 7; j++)
{
if ((short)crc_reg < 0)
crc_reg = (short)((crc_reg << 1) ^ 0x1021);
else
crc_reg <<= 1;
}
}
return crc_reg;
}

算法三:
public static int Crc_ccitt_good(byte[] sz) {
int poly = 0x1021;
short xor_flag;
short good_crc = (short) 0xffff;
int len = sz.length;
for (int len1 = 0; len1 < len; len1++) {// 数据总长度
short i, v;
v = 0x80;
for (i = 0; i < 8; i++) { // 处理完8位数据
if ((good_crc & 0x8000) > 0) // 判断CRC寄存器最高位是否为1
xor_flag = 1;
else
xor_flag = 0;
good_crc = (short) (good_crc << 1); // CRC寄存器左移一位,
// 把要处理的数据移入CRC寄存器
if ((sz[len1] & v) > 0) // 判断要移入CRC寄存器的数据位是否为1
good_crc = (short) (good_crc + 1);// 为1则CRC寄存器+1,为0则不加
if (xor_flag > 0) // 移出的数据为1,则CRC寄存器与多项式相异或
good_crc = (short) (good_crc ^ poly);
v = (short) (v >> 1); // 指向移入CRC寄存器的数据位
}
}
for (int j = 0; j < 16; j++) {// 这下面的程序是干吗用的?按说上面那段程序得到的
// good_crc就是CRC校验和吧?不知我理解的对不?
if ((good_crc & 0x8000) > 0)
xor_flag = 1;
else
xor_flag = 0;
good_crc = (short) (good_crc << 1);
if (xor_flag > 0)
good_crc = (short) (good_crc ^ poly);
}
return good_crc;
}

算法四:
	public static short cal_crc(byte[] ptr) {
char i;
short crc = 0;
int len = ptr.length;
for (int index = 0; index < len; index++) {
for (i = 0x80; i != 0; i /= 2) {
if ((crc & 0x8000) != 0) {/* 余式CRC乘以2再求CRC */
crc *= 2;
crc ^= 0x1021;
} else
crc *= 2;
if ((ptr[index] & i) != 0)
crc ^= 0x1021; /* 再加上本位的CRC */
}
}
return crc;
}

算法五:
	public static int java( byte[] bytes) { 
int crc = 0xFFFF; // initial value
int polynomial = 0x1021; // 0001 0000 0010 0001 (0, 5, 12)

// byte[] testBytes = "123456789".getBytes("ASCII");

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;
// System.out.println("CRC16-CCITT = " + Integer.toHexString(crc));
return crc;

}
悠游一叶 2010-02-04
  • 打赏
  • 举报
回复
算法一:
	public static int CRC_CCITT1(byte d[]){
byte b = 0;
short crc = (short)0xffff;
int i, j;
int len = d.length;

for(i=0; i<len; i++){
for(j=0; j<8; j++){
b = (byte)(((d[i]<<j)&0x80) ^ ((crc&0x8000)>>8));
crc<<=1;
if(b!=0)
crc^=0x1021;
}
}
crc = (short)~crc;
return crc&0xffff;
}

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧