# Thread: QByteArray sum

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

## QByteArray sum

Guys,
is there any way to calculate sum of QByteArray, not as integer but byte... so max value must be 255...
i want to avoid any mistake from
Qt Code:
`sum % 256`
To copy to clipboard, switch view to plain text mode

2. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

A byte is nothing more than a group of bits that define a unit of digital data.
It is true that de facto today 8 bit bytes are the common case, but there is no standard defining it.
The size of a byte is hardware define.

In short, you want the values in ints that are 8 bits wide, commonly represented by 'char' (or unsigned char).

The requirement makes little sense.
Is it guaranteed that the count of the items in your QByteArray and the sum of their values really is not larger than 255??
I strongly doubt that.
And if it is guaranteed, than the type in which you store your sum can be any larger integer type, it wont matter - the sum will not exceed 255

Maybe if you'd explain what is the problem you are trying to solve with summing your QByteArray we could help you to come to the correct solution.

3. ## The following user says thank you to high_flyer for this useful post:

FalloutST (2nd April 2017)

4. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

for(i=0; bla bla) {
bytearray[0] = bytearray[0] + byteadday2[i];
}
that is the way I did it,
both of us, I guess, expect value 256 in byte as 0, overflow will do it's work
11111111 = 255
100000000 = 256

AVR chip work with bytes and it is sensitive to push it to 16.
I am already using 68% of RAM it have.
It count sum of received data bytes, but c++ do so with "Ñrutch" at the top of the post.
I guess there must be other... "true" way to do so...

5. ## Re: QByteArray sum

It sounds like you are confused in your requirements. Do you want to:

- count the number of bytes in the byte array
- compute the sum of the byte values in the array
- do something else?

In the first two cases, either of these could be greater than 255, even if the value of any byte in the array has the range 0 <= byte <= 255.

The fake code you show in your last post won't compile and doesn't even make a lot of sense. If you want real help on this forum, have some respect for the people who are willing to help you by at least posting a piece of your actual code so we can try to understand what you might be doing wrong.

6. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

1) second
2)
Qt Code:
`qDebug() << "data3=" << data;    for (int i = 0; i < sp_dataLength; i++) {       bytearray[0] = bytearray[0] + sp_ar_dataToSend[i]; // QByteArray sp_ar_dataToSend;    }    data.append((const char*)(bytearray), sizeof(char));`
To copy to clipboard, switch view to plain text mode
compiled, work fine
not tested with overflow yet

7. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

compiled, work fine
Well, depends what you define as "works".
If all your bytes contain the value 1, then sp_ar_dataToSend is not allowed to have more than 255 elements.
In such a case any larger array will give you false sum.
Another case is when you array holds bytes that are considerably larger than 1, which in any real work situation is sure to be.
Lets say you have 3 bytes in the array each with the value of 100.
In that case your sum is definitely wrong.
Also note I totally neglected the fact you always add the last sum to the new value, this makes your sum calculation probably wrong all the time since it limits the values you can add even more.
So the code will run since it is syntactically correct (hence you could say "work"), but the sum most certainly will be false.

May we ask what is the purpose of this sum?
Are you trying to make a checksum so that the receiver can be certain the data is correct or maybe so that the receiver can allocate enough buffer memory?
(if this is a checksum, there are some nice examples on the Internet maybe the AVR lib has a function for it as well)

I am already using 68% of RAM it have.
It sounds to me that because you want your 0 array item to hold the sum of bytes in the array, you are limited to use an 8bit byte to store the sum.
Why don't you send a struct instead of the array that will hold both informations:
Qt Code:
`sturct dataPacket {long int sum;QByteArray data;};`
To copy to clipboard, switch view to plain text mode

This way you are not limited to the 8bit byte as a holder for your sum.
Of course you should know what the largest sum you can have (0xFF * size of array) and make sure its less than what the long int can hold on your AVR.

8. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

AVR
Qt Code:
`for (int i = 1; i < sp_dataLength; i++) {       sum += sp_recievedData[i];   }`
To copy to clipboard, switch view to plain text mode
Qt
Qt Code:
`for (int i = 0; i < sp_dataLength; i++) {       bytearray[0] = bytearray[0] + sp_ar_dataToSend[i];    }`
To copy to clipboard, switch view to plain text mode
output
sp_recievedData[i] : 1, 10, 215, 35, 60
result sum: 65
result bytearray[0] : 65

bytearray[0] - is QByteArray same as dataToSend

1+10+215+35+60 = 321 - 256 = 65

May I ask you?
If so: why should I count it in integer if it is loss prevention protocol?

My question is replacement bytearray[0] by other variable, in QT, to operate BYTE. CHAR crush....

I can't say it other "clear" way.

Join Date
Dec 2009
Location
New Orleans, Louisiana
Posts
791
Thanks
13
Thanked 153 Times in 150 Posts
Qt products
Platforms

## Re: QByteArray sum

There is no native 8-bit integer type. Qt provides quint8, which is guaranteed to be an 8-bit unsigned value on any platform, but it's effectively an unsigned char type. So, use quint8 or an unsigned char if you *must* compute the sum using a single byte.

In my opinion, using an integer type for the sum and computing the remainder using sum % 256 is more straight forward in my opinion.

10. ## The following user says thank you to jefftee for this useful post:

FalloutST (3rd April 2017)

11. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

@FalloutST: Ok, I have the feeling I am totally not understanding what you want or doing, maybe its a language barrier.
1+10+215+35+60 = 321 - 256 = 65
I don't understand what this is giving you.
What is 65?
What do you do with it?

12. ## The following user says thank you to high_flyer for this useful post:

FalloutST (3rd April 2017)

13. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

Originally Posted by jefftee
There is no native 8-bit integer type. Qt provides quint8, which is guaranteed to be an 8-bit unsigned value on any platform, but it's effectively an unsigned char type. So, use quint8 or an unsigned char if you *must* compute the sum using a single byte.

In my opinion, using an integer type for the sum and computing the remainder using sum % 256 is more straight forward in my opinion.
I share code in previous post it work way I want.

Added after 6 minutes:

Originally Posted by high_flyer
@FalloutST: Ok, I have the feeling I am totally not understanding what you want or doing, maybe its a language barrier.

I don't understand what this is giving you.
What is 65?
What do you do with it?
There only 10 types of the people, those who understand binary and doesn't.

Try to read what I asked.. Mr. Putin now in same situation, so be stronger

wow you'll get that: I need BYTE variable overflowing without crush. To compute sum of Byte array same way AVR does.
Last edited by FalloutST; 3rd April 2017 at 12:26.

14. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

There only 10 types of the people, those who understand binary and doesn't.
Listen, mocking the ones who are taking the time and are trying to help you wont help much.
I understand binary alright, what I don't understand is your very broken english and lack of sense in your text.
That's ok, I don't have to.

15. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

I am thankful even for reading my posts, and pressed special "Thank you" button. With pleasure.
Unfortunately your conversation is win-lose way. My joke is about reading questions.
I need BYTE variable overflowing without crush. To compute sum of Byte array same way AVR does.
Lets go that way, you English is better than my. You right. You won.

16. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

Unfortunately I can't make sense of your question.
The sentence simply makes no sense to me.
The integer overflow per se should not crash, the crash is probably somewhere else (but related to the wrong sum you have).
I don't know what byte variable overflowing without crush means, sorry.
I tried asking you some questions to help clarify what you mean, but you simply ignored them.
You didn't say where your application is crashing.
Since you didn't answer my questions what is the sum used for, I can only guess you are using it to allocate a buffer - if this is correct, then it could be the issue is with code expecting the buffer to be other size, or loop iterating it or something similar.
But I don't know since you do not answer any of the questions you are being asked, you only ignore them and repeat what you already said.
I also suggested to you to use a struct instead of the 0 item of your QByteArray, you ignored that as well.
Answer the open questions and maybe it will help us help you further.
Last edited by high_flyer; 3rd April 2017 at 18:05.

17. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

OK I'll try one more time...
I do not care about right human meaning of sum of elements I have in dynamical array. That array is received by seial port bytes.
parsing that package I need to be sure received bytes are sent from one device. In my case sender device can send other information it received from other ports. In that case bytes could pass true directly to parser. To be sure package is OK I need to check sum. Chance it happen is very small that why I decided to count bytes as insurance it is save to parse it. Not checksum working same way by the way.
Next---> it is easy to just count array[1]+ array[2] +arr.... and send that info.
In my case is lack of memory says me to save even byte... so that sum I want store in BYTE not LONG lot VERY LONG not any other just unsigned byte.
Math says to me that byte would be same:
1+10+215+35+60 = 321 - 256 = 65
256 is byte -------> 0 to 255 is 256 man!!!!
so if u'll count bytes 255+255+255+255+255 it will be in byte 251 not sure but... must be
so the best C++ way, I've found, to do so is:
bytearray[0] = bytearray[0] + sp_ar_dataToSend[i]
to your attention not bytearray[0] += sp_ar_dataToSend[i] initialization will return wrong val.
I am sure it is not good to initialize other, byte array variable to count one byte.
You can try to use char and if it will work, or you'll find other variable remind me.
sum % 256 might be ok but:
I am sure it is not good to initialize other, byte array variable to count one byte.
same as long float and other.
But to use LONG and than count % is absolutely wrong way, same as
Qt Code:
`int sum += sp_ar_dataToSend[i] for (int i = sum; i > 256;) {sum-=256;i=sum;}data.append((const char*)(&sum), sizeof(int));`
To copy to clipboard, switch view to plain text mode

Last edited by FalloutST; 4th April 2017 at 15:07.

18. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

I am sorry, I simply can't follow.
I got that you are doing some kind of a check sum, fair enough.
But everything else I just cant understand.
The code you posted wont compile and I can't understand it.
It *seems* to me you are very sure about your math, and yet, it appears to me that you still have a problem with it, but what that problem is I just can't make out of your post, or what the actual problem or question is.
Maybe someone else here understands more and can help.
Sorry.

EDIT:
maybe if you post a bit more of your code, the real code, where you calculate your checksum, and where you use, it will be clearer.
Last edited by high_flyer; 4th April 2017 at 16:35.

19. Beginner
Join Date
Apr 2017
Posts
10
Thanks
3
Qt products
Platforms

## Re: QByteArray sum

int sp_dataLength;
QByteArray bytearray;
QByteArray sp_ar_dataToSend;
for (int i = 0; i < sp_dataLength; i++) {
bytearray[0] = bytearray[0] + sp_ar_dataToSend[i];
}

real ctrl+c ctrl+v
You need any other code to find out any other byte variable to work same way?
ok next line:
data.append((const char*)(bytearray), sizeof(char));
and other line is:
qDebug() << "data4=" << data;
qDebug() << "sp_ar_dataToSend =" << sp_ar_dataToSend;
may you say please what part of it you need?

Added after 6 minutes:

There is no native 8-bit integer type. Qt provides quint8, which is guaranteed to be an 8-bit unsigned value on any platform, but it's effectively an unsigned char type. So, use quint8 or an unsigned char if you *must* compute the sum using a single byte.

In my opinion, using an integer type for the sum and computing the remainder using sum % 256 is more straight forward in my opinion.
I'll return to quint8 may be, right after high_flyer
Last edited by FalloutST; 4th April 2017 at 22:32.

20. ## Re: QByteArray sum

int sp_dataLength;
QByteArray bytearray;
QByteArray sp_ar_dataToSend;
for (int i = 0; i < sp_dataLength; i++) {
bytearray[0] = bytearray[0] + sp_ar_dataToSend[i];
}
Like high_flyer, I don't understand at all what you are trying to do. Your explanations go in circles.

But if this is your actual code, and it is what is crashing, then it is probably because:

1 - sp_dataLength is not initialized, so it has some random value.
2 - bytearray is not initialized, so it has zero length.
3 - sp_ar_dataToSend is also not initialized, so it also has zero length
4 - accessing bytearray[0] can crash because it does not have an entry at index 0 (it has zero length)
5 - accessing sp_ar_dataToSend[i] can crash because it has no entries at any index "i" (it also has zero length)
6 - even if you initialize bytearray to some non-zero length, if you do not also set bytearray[0] = 0 before you start the for() loop, then the sum will be wrong.

21. Wiseguy
Join Date
Jan 2006
Location
Munich, Germany
Posts
4,716
Thanks
21
Thanked 418 Times in 411 Posts
Qt products
Platforms

## Re: QByteArray sum

In addition to all the valid points d_stranz made one more:
- assuming you did initialized bytearray (point 2 in d_stranz's post)
You only use the '0' element - so why use an array at all and not just a quint8?'
(I am totally not getting in to the logic of the code it self just about the "technical" aspect of it)
The following code will be equivalint to yours ( in terms of not using 'bytearray')
Qt Code:
`int sp_dataLength;//QByteArray bytearray;quint8 sum = 0;QByteArray sp_ar_dataToSend;//initialize sp_ar_dataToSend and dataLength and then:for (int i = 0; i < sp_dataLength; i++) {sum += sp_ar_dataToSend.at(i); //using at() instead of '[]' is better as it does a check for the indey and will assert (instead of crash)}`
To copy to clipboard, switch view to plain text mode

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.