Java中的8位Fletcher算法的正确实现是什么? [英] what is the correct implementation of the 8-bit Fletcher algorithm in java?

查看:153
本文介绍了Java中的8位Fletcher算法的正确实现是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试实现8位fletcher算法。
我写了一段代码来做到这一点,但是我不确定我是否正确理解算法。
这是我的代码:

 公共类TestFletcher {
public static void main(String [ ] argv){

String bin = 1001001010111110110110110111001100;
char [] cA = bin.toCharArray();
int ckA = 0,ckB = 0;
for(int i = 0; i< cA.length; i ++){
ckA + = Integer.valueOf(cA [i])/ 49;
ckB + = ckA;
}
System.out.println(ckA);
System.out.println(ckB);

}

我得到的结果是:ckA = 20, ckB =308。
i假定这不是正确的实现,因为308不能用ckA和ckB的长度的8位二进制表示。



可以有人对这个问题有所了解吗?
任何帮助将不胜感激。谢谢。

解决方案

根据本文,您应该对ckA和ckB的值执行模数计算,以防止它们超过255。因此示例如下:

 字符串bin = 100100101011111011101011; 
char [] cA = bin.toCharArray();
int ckA = 0,ckB = 0;
for(int i = 0; i< cA.length; i ++){
ckA =(ckA + Integer.valueOf(cA [i])/ 49)%255;
ckB =(ckB + ckA)%255;
}
System.out.println(ckA);
System.out.println(ckB);

System.out.println((ckB<< 8)| ckA);

这可能主要是由于最终校验和是8位移位的ckB OR与ckA,因此ckA的值几乎肯定应该小于256。但是,除非您处理的是可能很大的二进制字符串,否则您可能只能在ckA上执行模数计算。


i am trying to implement the 8-bit fletcher algorithm. I wrote a piece of code that does that but i am not sure if i understood the algorithm correctly. this is my piece of code:

public class TestFletcher {
public static void main(String[] argv) {

    String bin = "10010010101111101110101101110011";
    char[] cA = bin.toCharArray();
    int ckA = 0, ckB = 0;
    for (int i = 0; i < cA.length; i++){
        ckA += Integer.valueOf(cA[i])/49;
        ckB += ckA;
    }
    System.out.println(ckA);
    System.out.println(ckB);

}

the results that i am getting are : ckA = 20, ckB = 308. i assume this is not the correct implementation since 308 can not be represented by an 8bit binary which is the length of ckA and ckB.

can any one shed some light on this problem? any help would be appreciated. thank you.

解决方案

According to this article, you should be performing modulus calculation on the values of ckA and ckB to prevent them from exceeding 255. So the example would be:

String bin = "100100101011111011101011";
char[] cA = bin.toCharArray();
int ckA = 0, ckB = 0;
for (int i = 0; i < cA.length; i++){
    ckA = (ckA + Integer.valueOf(cA[i])/49) % 255;
    ckB = (ckB + ckA) % 255;
}
System.out.println(ckA);
System.out.println(ckB);

System.out.println((ckB << 8) | ckA);

This is probably mostly due to the fact that the end checksum is a 8-bit shifted ckB ORed with ckA, and so the value of ckA should almost certainly be less than 256. However unless you're dealing with potentially large binary strings, you could probably get away with performing the modulus calculation only on ckA.

这篇关于Java中的8位Fletcher算法的正确实现是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆