Had asked a much broader question yesterday, but figured it may be prudent to delete it and narrow my question down a bit. Again, in the interest of honesty, yes, this is homework. I'm trying to develop a CRC algorithm with the following polynomial:
x15+x13+x6+x4+x1+1
I'm supposed to pass two bytes (8-bits), combine them into a 16-bit result (so I shift the first byte left 8, then add the two together), and then find the CRC using the above polynomial. I've been checking what my output should be using this tool, but I can't seem to get the correct answer.
What I have:
(Relevant Global Variables)
static String binaryCRC = "1010000001010011";
static long divisor = Long.parseLong(binaryCRC, 2);
static int mask = 0x8000;
(Actual Algorithm)
public static void crc(byte first, byte second) {
long total = ((first << 8) + second);
System.out.print(Long.toHexString(total));
for (int i = 0; i < binaryCRC.length(); i++) {
if ((total & mask) == mask) {
total ^= divisor;
}
total <<= 1;
}
System.out.println(" -> " + Long.toHexString(total));
}
EDIT: My attempt to revise my for-loop utilizing the suggestion given below:
for (int i = 0; i < binaryCRC.length(); i++) {
if ((total & mask) == mask) {
total = (total << 1) ^ divisor;
} else {
total <<= 1;
}
}
Perhaps I'm doing it incorrectly, but my output becomes incredibly far off when I do this way. When setting the value of my two bytes to the ASCII values of chars 'a' and 'b' (total = 6162), I get 6162 -> 4f1b065d, when I should be getting 77eb.
EDIT 2: I briefly outlined it below, but in the interest of clarity, I'm adding the rest of what I need to do, because I don't know how to find the cumulative CRC across multiple characters.
I need to find the cumulative CRC of the String below, and print the CRC thus far every 64th character. My answer is currently bf58 for the first 64, when the answer SHOULD be 1a6a.
public class test2 {
static String binaryCRC = "1010000001010011";
static long divisor = Long.parseLong(binaryCRC, 2);
static long cumCRC = divisor;
static long mask = 0x8000;
static long[] crcTable = new long[256];
static int counter = 0;
static String text = "abcdefghijklmnopqrstuvwxyz12345-ABCDEFGHIJKLMNOPQRSTUVWX"
+ "YZ12345abcdefghijklmnopqrstuvwxyz12345-ABCDEFGHIJKLMNOPQ"
+ "RSTUVWXYZ12345abcdefghijklmnopqrstuvwxyz12345-ABCDEFGHIJ"
+ "KLMNOPQRSTUVWXYZ12345abcdefghijklmnopqrstuvwxyz12345-ABC"
+ "DEFGHIJKLMNOPQRSTUVWXYZ12345abcdefghijklmnopqrstuvwxyz12"
+ "345-ABCDEFGHIJKLMNOPQRSTUVWXYZ12345abcdefghijklmnopqrstu"
+ "vwxyz12345-ABCDEFGHIJKLMNOPQRSTUVWXYZ12345.............."
+ "........................................................"
+ "........................................................"
+ "000075dc";
static char[] chars = text.toCharArray();
public static void main(String[] args) {
for (int i = 0; i < chars.length - 8; i += 2) {
crc((byte)chars[i], (byte)chars[i + 1]);
System.out.print(chars[i] + "" + chars[i+1]);
//Probably wrong
cumCRC = ((cumCRC >> 8) ^ crcTable[i / 2]) & 0xFFFF;
if ((i + 2) % 64 == 0) {
System.out.println(" - " + Long.toHexString(cumCRC));
}
}
}
public static void crc(byte first, byte second) {
long total = ((first << 8) + second);
//System.out.print(Long.toHexString(total));
for (int i = 0; i < binaryCRC.length(); i++) {
if ((total & mask) != 0) {
total = (total << 1) ^ divisor;
} else {
total <<= 1;
}
}
//System.out.println(" -> " + Long.toHexString(total));
crcTable[counter] = total;
counter++;
}
}