Results 1 to 18 of 18

Thread: QByteArray sum

  1. #1
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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:
    1. sum % 256
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================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.

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

    FalloutST (2nd April 2017)

  4. #3
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== 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.

  6. #5
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QByteArray sum

    1) second
    2)
    Qt Code:
    1. qDebug() << "data3=" << data;
    2. for (int i = 0; i < sp_dataLength; i++) {
    3. bytearray[0] = bytearray[0] + sp_ar_dataToSend[i]; // QByteArray sp_ar_dataToSend;
    4. }
    5. 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. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    1. sturct dataPacket {
    2. long int sum;
    3. };
    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.

  8. #7
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QByteArray sum

    AVR
    Qt Code:
    1. for (int i = 1; i < sp_dataLength; i++) {
    2. sum += sp_recievedData[i];
    3. }
    To copy to clipboard, switch view to plain text mode 
    Qt
    Qt Code:
    1. for (int i = 0; i < sp_dataLength; i++) {
    2. bytearray[0] = bytearray[0] + sp_ar_dataToSend[i];
    3. }
    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.

  9. #8
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default 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.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

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

    FalloutST (3rd April 2017)

  11. #9
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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?
    ==========================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.

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

    FalloutST (3rd April 2017)

  13. #10
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default Re: QByteArray sum

    Quote Originally Posted by jefftee View Post
    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:


    Quote Originally Posted by high_flyer View Post
    @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. #11
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================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.

  15. #12
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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. #13
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================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.

  17. #14
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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:
    1. int sum += sp_ar_dataToSend[i]
    2. for (int i = sum; i > 256;) {
    3. sum-=256;
    4. i=sum;
    5. }
    6. 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. #15
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    ==========================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.

  19. #16
    Join Date
    Apr 2017
    Posts
    10
    Thanks
    3
    Qt products
    Qt5
    Platforms
    Unix/X11

    Default 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. #17
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== 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.

  21. #18
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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:
    1. int sp_dataLength;
    2. //QByteArray bytearray;
    3. quint8 sum = 0;
    4. QByteArray sp_ar_dataToSend;
    5. //initialize sp_ar_dataToSend and dataLength and then:
    6. for (int i = 0; i < sp_dataLength; i++) {
    7. 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)
    8. }
    To copy to clipboard, switch view to plain text mode 
    ==========================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.

Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 13:14
  2. Replies: 1
    Last Post: 22nd June 2011, 09:12
  3. Regarding qbytearray
    By mohanakrishnan in forum Qt Programming
    Replies: 7
    Last Post: 19th November 2009, 14:38
  4. Replies: 9
    Last Post: 25th July 2009, 14:27
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 07:16

Bookmarks

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.