Your description of the process is wrong but you have given the correct result in both cases.
1 - "01050500FF00", LRC should be F6
0+1+0+5+0+5+0+0+F+F+0+0 = 010A // wrong working, right answer
0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A // this is the correct working
FF - 0A = F5
F5 +1 = F6
1 - "01050500FF00", LRC should be F6
0+1+0+5+0+5+0+0+F+F+0+0 = 010A // wrong working, right answer
0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A // this is the correct working
FF - 0A = F5
F5 +1 = F6
To copy to clipboard, switch view to plain text mode
Steps 2 and 3 are a description of negating a number in Two's-complement. Your computer will do this for you. Here is your example with the LRC appended to the input:
// Convert to binary
// Step 1, sum bytes (relies on char losing any overflow)
char sum = 0;
foreach (char c, work)
sum += c;
// steps 2 and 3: negate result
sum = -sum;
qDebug() << "Input =" << work.toHex().toUpper();
work.append(sum);
qDebug() << "With LRC =" << work.toHex().toUpper();
QByteArray input("01050500FF00");
// Convert to binary
QByteArray work = QByteArray::fromHex(input);
// Step 1, sum bytes (relies on char losing any overflow)
char sum = 0;
foreach (char c, work)
sum += c;
// steps 2 and 3: negate result
sum = -sum;
qDebug() << "Input =" << work.toHex().toUpper();
work.append(sum);
qDebug() << "With LRC =" << work.toHex().toUpper();
To copy to clipboard, switch view to plain text mode
Bookmarks