Results 1 to 18 of 18

Thread: QByteArray sum

Hybrid View

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

    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.

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

    FalloutST (2nd April 2017)

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

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

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,349
    Qt products
    Qt5
    Platforms
    Windows
    Thanks
    318
    Thanked 872 Times in 859 Posts

    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.

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

    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

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

    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.

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

    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.

Similar Threads

  1. Replies: 6
    Last Post: 14th May 2014, 12:14
  2. Replies: 1
    Last Post: 22nd June 2011, 08:12
  3. Regarding qbytearray
    By mohanakrishnan in forum Qt Programming
    Replies: 7
    Last Post: 19th November 2009, 13:38
  4. Replies: 9
    Last Post: 25th July 2009, 13:27
  5. QByteArray in Qt3
    By joseph in forum Qt Programming
    Replies: 1
    Last Post: 6th September 2007, 06: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
  •  
Qt is a trademark of The Qt Company.