1. Beginner
Join Date
Apr 2014
Posts
3
Thanks
1
Qt products
Platforms

## HEX addition, subtraction LRC ASCII

Trying to do ASCII LRC calculation, so I have hex values stored in a QByteArray, something in the lines of 01050500FF00, I need to add these values together, then subtract the result from FF, and add 1 to get the LRC..

in short :
1- Step1 = Sum Hex bytes of given QByteArray
2- Step2 = "FF" - Step1 (take the right part)
3- Step3 = Step2 + 1

Example :

1 - "01050500FF00", LRC should be F6
0+1+0+5+0+5+0+0+F+F+0+0 = 010A
FF - 0A = F5
F5 +1 = F6

2 - "010505000000", LRC should be F5
ie 0+1+0+5+0+5+0+0+0+0+0+0 = 0B
FF - 0B = F4
F4 + 1 = F5

My question is there an easy way to do Hex addition and subtraction in QT? if so how? or do I convert to decimal... case 'A' : a[i] = 11;break;... and so on..

tnx for any help..

p.s. C++
Last edited by phakorr; 28th April 2014 at 10:48. Reason: added p.s. to tell that this is for C++

2. Expert
Join Date
Mar 2008
Location
Kraków, Poland
Posts
1,532
Thanked 281 Times in 276 Posts
Qt products
Platforms

## Re: HEX addition, subtraction LRC ASCII

Did You have in QByteArray characters ('0'..'9', 'A'..'F') or bytes (0..0xf) ? This is not the same.

3. Beginner
Join Date
Apr 2014
Posts
3
Thanks
1
Qt products
Platforms

## Re: HEX addition, subtraction LRC ASCII

Supposed to have bytes..

The QByteArray crcWord is formed as such.. where ;

ui->lineEdit->text() is '01' (slave id)
functioncode is '05' (modbus function code)
ui->lineEdit_2->text() is '0500' (Register address hex)
ui->lineEdit_3->text() is '0000' (or 'FF00' from the examples I gave)
(data)

Qt Code:
`QByteArray crcWord = QString(ui->lineEdit->text() + functionCode + ui->lineEdit_2->text() + ui->lineEdit_3->text()).toLatin1();`
To copy to clipboard, switch view to plain text mode

I later write to the port, it all works when I manually add the LRC manually (i.e. replace the crc.toUpper() with "F5" (or whatever the LRC value of the word is) )

Qt Code:
`.. QString sendWord = ":" + crcWord + crc.toUpper() + "\r\n"; QByteArray sndwrd = sendWord.toLatin1();const char *cstr = sndwrd.data(); serial->write(cstr);serial->close();`
To copy to clipboard, switch view to plain text mode
Last edited by phakorr; 28th April 2014 at 16:54. Reason: added info on values..

4. Novice
Join Date
Oct 2013
Posts
41
Thanks
1
Thanked 8 Times in 7 Posts
Qt products
Platforms

## Re: HEX addition, subtraction LRC ASCII

Is this what you are trying to implement?

http://en.wikipedia.org/wiki/Longitu...dundancy_check

If so, you'll need to take your byte array which contains a string, and pull 2 characters out of it at a time to convert them to bytes. Then do your calculations on the bytes.

5. Expert
Join Date
Mar 2008
Location
Kraków, Poland
Posts
1,532
Thanked 281 Times in 276 Posts
Qt products
Platforms

## Re: HEX addition, subtraction LRC ASCII

So in QByteArray You have ASCII characters (string) not binary data. You can convert them to binary data with QByteArray::fromHex method and then simply add byte by byte.

6. Guru
Join Date
Mar 2009
Location
Brisbane, Australia
Posts
7,734
Thanks
13
Thanked 1,610 Times in 1,537 Posts
Qt products
Platforms
Wiki edits
17

## Re: HEX addition, subtraction LRC ASCII

Your description of the process is wrong but you have given the correct result in both cases.
Qt Code:
`1 - "01050500FF00", LRC should be F60+1+0+5+0+5+0+0+F+F+0+0 = 010A  // wrong working, right answer0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A   // this is the correct workingFF - 0A = F5F5 +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:
Qt Code:
`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

7. ## The following user says thank you to ChrisW67 for this useful post:

phakorr (29th April 2014)

8. Beginner
Join Date
Apr 2014
Posts
3
Thanks
1
Qt products
Platforms

## Re: HEX addition, subtraction LRC ASCII

Originally Posted by ChrisW67
Your description of the process is wrong but you have given the correct result in both cases.
Qt Code:
`..0+1+0+5+0+5+0+0+F+F+0+0 = 010A  // wrong working, right answer0x01 + 0x05 + 0x05 + 0x00 + 0xFF + 0x00 = 0x010A   // this is the correct working...`
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:
....
Thanks for the reply, all is working now. I wrote the calculation wrong partially on purpose as oddly enough it still calculates the LRC correctly for all the samples that I tried it on..