Recently, I created a small XOR checksum which allegedly worked (or at least in my head it did). I do not know what I have done wrong as I am following the exact same logic I used, when I have did it in C#/Java.
At the moment, it outputs rubbish values such as -87, -88 when the ints are sign and 2287800, 3485000 when unsigned. Is there a particular reason for that to happen?
Input data:
QByteArray str;
str[0] =0xAA;
str[1] =0xBB;
str[2] =0xCC;
str[3] =0xDD;
str[4] =0xFF;
str[5] =0x00;
char ch = checkSum(str).data()[0];
str[6] = ch;
serial.write(str, 7);
checkSum described bellow:
QByteArray MainWindow::checkSum(QByteArray &b)
{
qint16 b_len = b.length();
printf("b_length = %d\n", b_len);
char val = 0x00;
for (
int i = 0 ; i < b_len ; i ++ ){
val ^= b[i];
printf("Current: %d %d\n", val, b[i]);
}
return b;
}
Some output:
b_length = 6
Current: -88 1939120
Current: -81 1939120
Current: -80 1939120
Current: -69 1939120
Current: -70 1939120
Current: -70 1939120