QByteArray checksum calc issue
Unsolved
General and Desktop
-
Hello,
I had a network app that transmits data using a QByteArray. Part of my protocol has a "checksum" which is simply a quint16 sum of the bytes in the QByteArray. The checksum is then added to the end of the QByteArray before the data is transmitted:
QByteArray ba; // initialize the byte array // calc the checksum quint16 checksum = 0; for (int i = 0; i < ba.size(); i++) checksum += ba[i]; // add the checksum to the byte array ba.append((checksum >> 0) & 0xff); ba.append((checksum >> 8) & 0xff);
The issue I am seeing is that the checksum is not getting calculated properly. For example:
ba[0] = 0x11; // expected checksum = 0x0011 calculated = 0x0011 ba[1] = 0x0b // expected checksum = 0x001c calculated = 0x001c ba[2] = 0x01; // expected checksum = 0x001d calculated = 0x001d ba[3] = 0x27; // expected checksum = 0x0044 calculated = 0x0044 ba[4] = 0xfa; // expected checksum = 0x013e calculated = 0x003e !!!
Any ideas why the calculated checksum is dropping the upper byte?
Note, it does seem that as long as the byte array data is < 0x80 it does get added correctly. It only messes up if the data is >= 0x80.
Thanks
-
@bigguiness said in QByteArray checksum calc issue:
checksum += ba[i];
QByteArray is a 'signed' char - convert it properly.