How do I know a Longitudinal redundancy check is correctly calculated and how is it used to ensure that data before is not corrupt?
This is how I'm calculating it right now in Java but how do I know I am calculating it correctly given the data?
byte[] testMessage1 = {0x02, // STX
0x10,0x2,0xA,0x10,0x10,0x7,0x8, // Data 02, A, 10, 7, 8
0x03, // ETX
0x2^0xA^0x10^0x7^0x8^0x03}; // LRC calculated from the data (with the DLE removed) plus the ETX
public static byte calculateLRC2(byte[] bytes) {
byte checksum = 0;
for (int i = 1; i < bytes.length - 1; i++) {
checksum ^= (bytes[i] & 0xFF);
}
return checksum;
}