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
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
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
FalloutST (2nd April 2017)
that is the way I did it,for(i=0; bla bla) {
bytearray[0] = bytearray[0] + byteadday2[i];
}
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...
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.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
1) second
2)
compiled, work fineQt 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
not tested with overflow yet
Well, depends what you define as "works".compiled, work fine
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)
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.I am already using 68% of RAM it have.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
AVR
QtQt Code:
for (int i = 1; i < sp_dataLength; i++) { sum += sp_recievedData[i]; }To copy to clipboard, switch view to plain text mode
outputQt 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
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.
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 write the best type of code possible, code that I want to write, not code that someone tells me to write!
FalloutST (3rd April 2017)
@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.1+10+215+35+60 = 321 - 256 = 65
What is 65?
What do you do with it?
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
FalloutST (3rd April 2017)
I share code in previous post it work way I want.
Added after 6 minutes:
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 11:26.
Listen, mocking the ones who are taking the time and are trying to help you wont help much.There only 10 types of the people, those who understand binary and doesn't.
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.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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.
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 17:05.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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:
256 is byte -------> 0 to 255 is 256 man!!!!1+10+215+35+60 = 321 - 256 = 65
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:
same as long float and other.I am sure it is not good to initialize other, byte array variable to count one byte.
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 14:07.
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 15:35.
==========================signature=============== ==================
S.O.L.I.D principles (use them!):
https://en.wikipedia.org/wiki/SOLID_...iented_design)
Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.
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:
I'll return to quint8 may be, right after high_flyerThere 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.
Last edited by FalloutST; 4th April 2017 at 21:32.
Like high_flyer, I don't understand at all what you are trying to do. Your explanations go in circles.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];
}
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.
<=== The Great Pumpkin says ===>
Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.
Bookmarks