0

i have wrote a method that should calculate Fletcher checksum for a binary string. i used the answers provided in this question . here is the complete code:

String str = "101010100011001111001111";
int[] C = new int[2];
int indx = 0;
for (int i = 0; i < str.length()/8; i++){
    int tmp = Integer.parseInt(str.substring(indx, indx+8),2);
    C[0] = (C[0] + (tmp & 0xFF)) % 255;
       C[1] = (C[1] + C[0]) % 255;
    indx += 8;
}

    System.out.println(C[0]);
    System.out.println(C[1]);
    System.out.println((C[1] << 8) | C[0]);

the results that i am getting are:

173

54

13997

based on this article the final result should be 0 if the checksum performed correctly, and as you can see it is not. can any one help me understand where the problem might be? i appreciate you help. thank you.

Community
  • 1
  • 1
  • Please correct me if I'm wrong but shouldn't it be `indx+7` instead of `indx+8`? Also, what is `bt` and where have you used `tmp`? – user2004685 Mar 13 '16 at 08:33
  • I don't see `bt` defined anywhere. Did you mean to use `tmp` there? – radoh Mar 13 '16 at 08:41
  • yes, my bad. changed it. the indexing are fine. i have the same problem when i take a regular sting and then turn it to byte array with getbytes(), so the problem is not in the indexing. –  Mar 13 '16 at 09:04

0 Answers0