I'm a university student currently programming an assignment. I need to program various methods for sending and receiving messages, like for example the Hamming algorithm and Cyclic Redundancy Check.
I'm trying to program the CRC method on the transmitter end, but I can't manage to successfully program the polynomial division required. I've tried several solutions posted here, like using BitSet for division, to no avail.
Since I'm working with a graphical interface, designed in NetBeans 8.0.1, my question is: how can I manipulate Strings coming from several jTextFields to generate a binary message with the CRC algorithm?
Note: this is the first time I'm using Stack Overflow, so if I'm missing anything, please point it out for me. Thanks in advance.
EDIT: As requested, here is my sample code using BitSet: (note: some variable names are in Spanish since I'm a native Spanish-speaker)
public static String CRC(String m, String G){
BitSet dividendo, divisor, divid1, divid2, resto, blanco;
dividendo = new BitSet(m.length());
divisor = new BitSet(G.length());
blanco = new BitSet(G.length());
blanco.clear();
for (int i = 0; i < m.length(); i++){
if(Integer.parseInt(m.substring(i, i+1)) == 1) {
dividendo.set(i);
} else {
dividendo.clear(i);
}
}
for (int i = 0; i < G.length(); i++){
if(Integer.parseInt(G.substring(i, i+1)) == 1) {
divisor.set(i);
} else {
divisor.clear(i);
}
}
divid1 = dividendo.get(0, divisor.length());
int largo1, largo2, largo3, largo4;
largo1 = dividendo.length();
largo2 = divisor.length();
largo3 = blanco.length();
largo4 = divid1.length();
for (int i = divisor.length(); i < dividendo.length(); i++) {
if (divid1.get(0) == divisor.get(0)){
divid1.xor(divisor);
divid2 = new BitSet(divid1.length());
for (int j = 1; j<divid1.length(); j++){
if(divid1.get(j))
divid2.set(j-1);
else
divid2.clear(j-1);
}
boolean valor = dividendo.get(i);
int largo5 = divid2.length();
divid2.set(divid2.length(), valor);
divid1 = divid2;
} else {
divid1.xor(blanco);
divid2 = new BitSet(divid1.length());
for (int j = 1; j<divid1.length(); j++){
if(divid1.get(j))
divid2.set(j);
else
divid2.clear(j);
}
boolean valor = dividendo.get(i);
divid2.set(divid2.length(), valor);
divid1 = divid2;
}
}
resto = new BitSet(divid1.length());
for (int j = 1; j<divid1.length(); j++){
if(divid1.get(j))
resto.set(j);
else
resto.clear(j);
}
String mFinal = dividendo.toString() + resto.toString();
return mFinal;
}
With inputs
String m = "10010101"
String G = "1011"
my expected output vs. actual output was
expected = 10010101010
actual = {0, 3, 5, 7}{1, 2, 3} (first array is the original message, second is the appended remainder)
with the code mentioned above.
I'm at a loss on what I'm doing wrong, so any kind of help will be appreciated.
CRCs are not calculated by doing polynomial divisions term-by-term. Polynomials over GF(2) and their division are how CRCs are defined and analyzed mathematically. The actual calculation is done with binary representations of the polynomials and states stored in machine integers, using rotations and exclusive-ors to operate on all the terms of the polynomials in parallel.
I recommend Ross Williams' tutorial on CRCs for a description of how you go from polynomials over GF(2) to exclusive-ors and rotates of binary strings.