0

I am trying to compute the crc checksum of two binary strings but I can only get the first iteration of the loop. Converting a string to a string array and then finally an int array. Error checking is done elsewhere.

public String checksum(String a, String b) {
    // These arrays will convert the input strings to an array
    String[] mArray = a.split("");
    String[] pArray = b.split("");

    // Creates arrays from the above corresponding arrays
    int[] mAr = new int[mArray.length];
    int[] pAr = new int[pArray.length];

    // populates message array
    for (int i = 0; i < a.length(); i++) {
        mAr[i] = Integer.parseInt(mArray[i]);

    }
    // populates pattern array
    for (int i = 0; i < b.length(); i++) {
        pAr[i] = Integer.parseInt(pArray[i]);
    }

    //int frame = mAr.length - pAr.length + 1;
    int pLength = pAr.length;
    int mLength = mAr.length;
//  int[] checksum = new int[frame];

    System.out.println(pLength);

 //CHECKSUM ITERATION
    for (int i = 0; i < pAr.length; i++) {
        mAr[i] = mAr[i] ^ pAr[i];
        if (i ==pLength) {
    //      mAr[i] = mAr[i] >> 1;
            i = 0;

        }
    }
    for (int i = 0; i < mAr.length; i++) {
        System.out.print(mAr[i]);
    }

My test is Message: 11001010 Pattern: 10011 Expected output: 0100 But instead, I get: 01010010 Which is the first xor of the two strings. But the loop won't continue to xor that string with pattern again. What can I do to correct this loop issue? Or am I going about this incorrectly?

  • 1
    `if (i ==pLength) {` – this branch will never be hit. – meowgoesthedog Oct 17 '18 at 14:25
  • This is just a XOR, not a CRC algorithm, continuing to XOR the pattern into the message would not make it a CRC algorithm it's a fundamentally different algorithm (XOR is *part* of it yes) – harold Oct 17 '18 at 14:28
  • If you want an array of characters, it's much cheaper to use `string.toCharArray()`. You can parse the integer in each with `ch - '0'`, though that has less error-checking. – Chai T. Rex Oct 17 '18 at 14:28
  • @meowgoesthedog yes, I just fixed that on my end to hit the branch – Ricardo96MC Oct 17 '18 at 14:31
  • @harold I understand that part but I can't get the final xor thus can"t continue with it edit: unless this is wrong? – Ricardo96MC Oct 17 '18 at 14:32
  • @ChaiT.Rex I figured if I am given a string and then make that into it an array of ints – Ricardo96MC Oct 17 '18 at 14:34
  • @Ricardo96MC You can do that with `for (int i = 0; i < string.length(); i++) { intArray[i] = string.charAt(0) - '0'; }`. Again, this removes the error checking, so you'll get weird results with `"hello"` instead of `"010101011"`. – Chai T. Rex Oct 17 '18 at 14:37
  • @Ricardo96MC a "repeating XOR" isn't even part of a CRC algorithm as far as I know but maybe you have some trick I don't know about. So even if you solve this and make it loop, CRC doesn't even use a step like that. At least, normally the algorithm is to *conditionally* XOR out a piece from the message, when you find a 1 in it and then use the XOR to remove that 1, keep going until in the end you're left with too little message to apply the XOR and that's the remainder – harold Oct 17 '18 at 14:37
  • @harold I see what you mean i got stuck xor for the Cyclic Redundancy Check checksum its (2^(n-k)D) / P = Q + (R/P) P = divisor D = message, k = message size, n = P size where R would be the resulting checksum. So I don't think I went about this correctly... – Ricardo96MC Oct 17 '18 at 14:44

1 Answers1

0

As people have mentioned, this might not help to implement a CRC algorithm, but to solve your specific request for help so that you know how to in the future, I'll include the answer.

Simple way

One simple but somewhat inefficient way to repeat pAr over and over again as needed is to loop until the end of mAr instead of pAr and use % on the index to reduce it to something in pAr:

for (int i = 0; i < mAr.length; i++) {
    mAr[i] = mAr[i] ^ pAr[i % pAr.length];
}

More efficient way

Something more complicated but likely more efficient (since branch prediction here is likely to work correctly most of the time while division to get the modulus is slow) is:

for (int i = 0, j = 0; i < mAr.length; i++) {
    mAr[i] = mAr[i] ^ pAr[j];

    j++;
    if (j == pAr.length) {
        j = 0;
    }
}

Using ^= to make things slightly more readable

Much like you can use +=, ^= is available as well, which is not only shorter but also helps the reader to very quickly know that mAr[i] is effectively on both the left and right of the equal sign without having to take extra thinking time to discover that:

for (int i = 0; i < mAr.length; i++) {
    mAr[i] ^= pAr[i % pAr.length];
}
for (int i = 0, j = 0; i < mAr.length; i++) {
    mAr[i] ^= pAr[j];

    j++;
    if (j == pAr.length) {
        j = 0;
    }
}
Chai T. Rex
  • 2,972
  • 1
  • 15
  • 33