# QByteArray HEX SUM

• Receiving a Packet Via UDB like this "10 04 02 00 06 10 03"
i want to remove the first hex number and the last two numbers and calculate the rest of number except the last number in the remaining frame (check sum)
the reminder Frame is 04 02 00 06
i cant calculate Sum of first 3 Hex number to = 06
code :

``````QByteArray UDBbuf = "10 04 02 00 06 10 03";
UDBbuf.replace( " ", "" );  //remove spaces
if (UDBbuf.contains("03"))
{
int loc = UDBbuf.indexOf("03",0);  //detect end of frame
qDebug() <<"Found at "<< loc;

if (loc == 12) {

UDBbuf=UDBbuf.left(loc);  //the the"03"

if(UDBbuf.startsWith("10") && UDBbuf.endsWith("10"))
{
UDBbuf = UDBbuf.right(UDBbuf.size() - 2);
UDBbuf = UDBbuf.left(UDBbuf.size() - 2);

QByteArray checksum= UDBbuf.mid(6,2);
QByteArray frame= UDBbuf.mid(0,6);

qDebug()<<"checksum="<< checksum;   // Resullt "06"
qDebug()<<"frame2 ="<< frame;  //Result  "04 02 00" iam stoped here i cant sum this number to get "06"

}
}
}
``````

Thanks,

• When it's really only a sum then you're looking for https://doc.qt.io/qt-5/qbytearray.html#fromHex - then you can add the bytes

• When it's really only a sum then you're looking for https://doc.qt.io/qt-5/qbytearray.html#fromHex - then you can add the bytes

• @Christian-Ehrlicher Thanks Alot its working.

but is there abetter Way To Parse This Message ?

``````        QByteArray text = QByteArray::fromHex(UDBbuf);

int CSBYTE =text.length()-1 ;
qDebug()<<"CSBYTE ="<< CSBYTE;
int calculatedSUM = (text + text +text) %256;
int DlevredSUM = text[CSBYTE];

if(calculatedSUM == DlevredSUM){
qDebug()<<"check Sum correct";
}

``````

• You are way overthinking this. For something as simple a an accumulated 8bit checksum over an array, simply iterate over the bytes in question and add their value to an 8bit accumulator defined as uint8_t. The overflow will have the side effect of an implicit mod 256 on the operation.

• Calling a sum a 'checksum' is an oxymoron :)