Lab Program 4
Lab Program 4
Lab Program 4
Aim: Develop a program for error detecting code using CRC-CCITT (16- bits).
// Calculation of CRC
for (int i = 0; i < messageLength; i++) {
if (data[i] == 1) {
for (int j = 0; j < divisor.length; j++) {
data[i + j] ^= divisor[j];
}
}
}
// Copy the input checksum into the data array for validation
int[] receivedData = new int[checksumLength + generatorLength - 1];
System.arraycopy(checksum, 0, receivedData, 0, checksumLength);
for (int i = 0; i < generatorLength; i++) {
divisor[i] = generator[i]; // Use the same generator
}
// Calculation of remainder
for (int i = 0; i < checksumLength; i++) {
if (receivedData[i] == 1) {
for (int j = 0; j < divisor.length; j++) {
receivedData[i + j] ^= divisor[j];
}
}
}
if (valid) {
System.out.println("Data stream is valid");
} else {
System.out.println("Data stream is invalid. CRC error occurred.");
}
sc.close();
}
}