Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QByteArray HEX SUM
Qt 6.11 is out! See what's new in the release blog

QByteArray HEX SUM

Scheduled Pinned Locked Moved Solved General and Desktop
5 Posts 3 Posters 1.0k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • M Offline
    M Offline
    Marco Flad
    wrote on last edited by
    #1

    Receiving a Packet Via UDB like this "10 04 02 00 06 10 03"
    i want to remove the first hex number and the last two numbers and calculate the rest of number except the last number in the remaining frame (check sum)
    the reminder Frame is 04 02 00 06
    i cant calculate Sum of first 3 Hex number to = 06
    code :

    QByteArray UDBbuf = "10 04 02 00 06 10 03";
    UDBbuf.replace( " ", "" );  //remove spaces
    if (UDBbuf.contains("03"))
    {
        int loc = UDBbuf.indexOf("03",0);  //detect end of frame
        qDebug() <<"Found at "<< loc;
    
        if (loc == 12) {   
        
        UDBbuf=UDBbuf.left(loc);  //the the"03" 
        
        if(UDBbuf.startsWith("10") && UDBbuf.endsWith("10"))
        {
            UDBbuf = UDBbuf.right(UDBbuf.size() - 2);
            UDBbuf = UDBbuf.left(UDBbuf.size() - 2);
           
            QByteArray checksum= UDBbuf.mid(6,2);
            QByteArray frame= UDBbuf.mid(0,6);
    
            qDebug()<<"checksum="<< checksum;   // Resullt "06"
            qDebug()<<"frame2 ="<< frame;  //Result  "04 02 00" iam stoped here i cant sum this number to get "06"
    
            }
        }
    }
    

    Thanks,

    1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      When it's really only a sum then you're looking for https://doc.qt.io/qt-5/qbytearray.html#fromHex - then you can add the bytes

      Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
      Visit the Qt Academy at https://academy.qt.io/catalog

      M 1 Reply Last reply
      2
      • Christian EhrlicherC Christian Ehrlicher

        When it's really only a sum then you're looking for https://doc.qt.io/qt-5/qbytearray.html#fromHex - then you can add the bytes

        M Offline
        M Offline
        Marco Flad
        wrote on last edited by
        #3

        @Christian-Ehrlicher Thanks Alot its working.

        but is there abetter Way To Parse This Message ?

                QByteArray text = QByteArray::fromHex(UDBbuf);
        
              
                int CSBYTE =text.length()-1 ;
                qDebug()<<"CSBYTE ="<< CSBYTE;
                int calculatedSUM = (text[0] + text[1] +text[2]) %256;
                int DlevredSUM = text[CSBYTE];
        
               if(calculatedSUM == DlevredSUM){
                    qDebug()<<"check Sum correct";
                }
        
        
        1 Reply Last reply
        0
        • Kent-DorfmanK Offline
          Kent-DorfmanK Offline
          Kent-Dorfman
          wrote on last edited by Kent-Dorfman
          #4

          You are way overthinking this. For something as simple a an accumulated 8bit checksum over an array, simply iterate over the bytes in question and add their value to an 8bit accumulator defined as uint8_t. The overflow will have the side effect of an implicit mod 256 on the operation.

          The dystopian literature that served as a warning in my youth has become an instruction manual in my elder years.

          1 Reply Last reply
          0
          • Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            Calling a sum a 'checksum' is an oxymoron :)

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            1 Reply Last reply
            0

            • Login

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • Users
            • Groups
            • Search
            • Get Qt Extensions
            • Unsolved