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. How to separate 3 bytes 3 bytes of QStringList

How to separate 3 bytes 3 bytes of QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 830 Views
  • 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.
  • I Offline
    I Offline
    isan
    wrote on last edited by isan
    #1

    I have a QStringList like:

    {FFD789FFCF92FFCECDFFD502FFD1FFFFCFE8FFCFC6FFD36BBB,
    FFDBE9FFD278FFD234FFD80EFFD644FFD3ECFFD281FFD692BB,
    FFE056FFD4AAFFD512FFD980FFDA52FFD701FFD4ADFFD88FBB,
    FFFA0EFFF190FFF133FFF669FFF47AFFF232FFF110FFF4F5BB,
    FFFD99FFF65FFFF59DFFFB8DFFF832FFF681FFF590FFF9CFBB,
    00022AFFF965FFF915FFFE9BFFFC91FFFA59FFF8F4FFFD16BB,
    000768FFFBC1FFFC3D00008900016BFFFDF2FFFBF3FFFFA4BB,
    000BBBFFFFF80000650004CA0005C100023900003A0003D3BB,
    000F0E000575000542000A6B0009730006B300054500092EBB,
    00117C000B1E000A38001044000C5B000AE5000A23000E7DBB,
    00153D000EEA000E0000142C001016000EBF000DF700125ABB,
    001A0500113800110B0016460014870012300010DA0014EABB,
    001E3C00147900148500196300189E0015D700145D001830BB,
    00215D0018C900186D001DA9001BD600197400184B001C35BB}
    

    I want to separate 3 bytes 3 bytes of each package and insert in to the new string/QStringList and then convert each of them to decimal .
    like this:

    //contain first 3 bytes of each package 
    QStringList one ={FFD789 ,FFDBE9,FFE056,FFFA0E,........};
    QList<double> appId1;
     appId1.append(hexastr2double(one.at(i)));
    
    .......
    
    //contain second 3 bytes of each package 
    QStringList two ={FFCF92,FFD278,FFD4AA,FFF190,....};
    QList<double> appId2;
     appId2.append(hexastr2double(one.at(i)));
    .
    .
    .
    //contain Eighth 3 bytes of each package 
    QStringList eight ={FFD36B,FFD692,FFD88F,FFF4F5,....}
    QList<double> appId2;
     appId2.append(hexastr2double(one.at(i)));
    
    double hexastr2double(const std::string& s) {
    
      double d = 0.0;
    
      sscanf(s.c_str(), "%lA", &d); // TODO Check for errors
    
      return d;
    }
    

    How can I do it?

    J.HilkJ 1 Reply Last reply
    0
    • I isan

      I have a QStringList like:

      {FFD789FFCF92FFCECDFFD502FFD1FFFFCFE8FFCFC6FFD36BBB,
      FFDBE9FFD278FFD234FFD80EFFD644FFD3ECFFD281FFD692BB,
      FFE056FFD4AAFFD512FFD980FFDA52FFD701FFD4ADFFD88FBB,
      FFFA0EFFF190FFF133FFF669FFF47AFFF232FFF110FFF4F5BB,
      FFFD99FFF65FFFF59DFFFB8DFFF832FFF681FFF590FFF9CFBB,
      00022AFFF965FFF915FFFE9BFFFC91FFFA59FFF8F4FFFD16BB,
      000768FFFBC1FFFC3D00008900016BFFFDF2FFFBF3FFFFA4BB,
      000BBBFFFFF80000650004CA0005C100023900003A0003D3BB,
      000F0E000575000542000A6B0009730006B300054500092EBB,
      00117C000B1E000A38001044000C5B000AE5000A23000E7DBB,
      00153D000EEA000E0000142C001016000EBF000DF700125ABB,
      001A0500113800110B0016460014870012300010DA0014EABB,
      001E3C00147900148500196300189E0015D700145D001830BB,
      00215D0018C900186D001DA9001BD600197400184B001C35BB}
      

      I want to separate 3 bytes 3 bytes of each package and insert in to the new string/QStringList and then convert each of them to decimal .
      like this:

      //contain first 3 bytes of each package 
      QStringList one ={FFD789 ,FFDBE9,FFE056,FFFA0E,........};
      QList<double> appId1;
       appId1.append(hexastr2double(one.at(i)));
      
      .......
      
      //contain second 3 bytes of each package 
      QStringList two ={FFCF92,FFD278,FFD4AA,FFF190,....};
      QList<double> appId2;
       appId2.append(hexastr2double(one.at(i)));
      .
      .
      .
      //contain Eighth 3 bytes of each package 
      QStringList eight ={FFD36B,FFD692,FFD88F,FFF4F5,....}
      QList<double> appId2;
       appId2.append(hexastr2double(one.at(i)));
      
      double hexastr2double(const std::string& s) {
      
        double d = 0.0;
      
        sscanf(s.c_str(), "%lA", &d); // TODO Check for errors
      
        return d;
      }
      

      How can I do it?

      J.HilkJ Offline
      J.HilkJ Offline
      J.Hilk
      Moderators
      wrote on last edited by
      #2

      @isan

      alt text

      int main (int argc, char *argv[])
      {
      
          const QStringList list{"FFD789FFCF92FFCECDFFD502FFD1FFFFCFE8FFCFC6FFD36BBB",
                      "FFDBE9FFD278FFD234FFD80EFFD644FFD3ECFFD281FFD692BB",
                      "FFE056FFD4AAFFD512FFD980FFDA52FFD701FFD4ADFFD88FBB",
                      "FFFA0EFFF190FFF133FFF669FFF47AFFF232FFF110FFF4F5BB",
                      "FFFD99FFF65FFFF59DFFFB8DFFF832FFF681FFF590FFF9CFBB",
                      "00022AFFF965FFF915FFFE9BFFFC91FFFA59FFF8F4FFFD16BB",
                      "000768FFFBC1FFFC3D00008900016BFFFDF2FFFBF3FFFFA4BB",
                      "000BBBFFFFF80000650004CA0005C100023900003A0003D3BB",
                      "000F0E000575000542000A6B0009730006B300054500092EBB",
                      "00117C000B1E000A38001044000C5B000AE5000A23000E7DBB",
                      "00153D000EEA000E0000142C001016000EBF000DF700125ABB",
                      "001A0500113800110B0016460014870012300010DA0014EABB",
                      "001E3C00147900148500196300189E0015D700145D001830BB",
                      "00215D0018C900186D001DA9001BD600197400184B001C35BB"};
      
          auto hexastr2double = [](const QString &str, int byteCount)->QList<int>{
              QList<int> l;
              for(int i(0),j(str.size()); i < j; i+=byteCount)
                  l.append(str.mid(i,3).toInt(nullptr, 16));
              return l;
          };
      
          for(const QString & str : list)
                  qDebug() << hexastr2double(str,3);
      
      }
      

      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      I 1 Reply Last reply
      4
      • J.HilkJ J.Hilk

        @isan

        alt text

        int main (int argc, char *argv[])
        {
        
            const QStringList list{"FFD789FFCF92FFCECDFFD502FFD1FFFFCFE8FFCFC6FFD36BBB",
                        "FFDBE9FFD278FFD234FFD80EFFD644FFD3ECFFD281FFD692BB",
                        "FFE056FFD4AAFFD512FFD980FFDA52FFD701FFD4ADFFD88FBB",
                        "FFFA0EFFF190FFF133FFF669FFF47AFFF232FFF110FFF4F5BB",
                        "FFFD99FFF65FFFF59DFFFB8DFFF832FFF681FFF590FFF9CFBB",
                        "00022AFFF965FFF915FFFE9BFFFC91FFFA59FFF8F4FFFD16BB",
                        "000768FFFBC1FFFC3D00008900016BFFFDF2FFFBF3FFFFA4BB",
                        "000BBBFFFFF80000650004CA0005C100023900003A0003D3BB",
                        "000F0E000575000542000A6B0009730006B300054500092EBB",
                        "00117C000B1E000A38001044000C5B000AE5000A23000E7DBB",
                        "00153D000EEA000E0000142C001016000EBF000DF700125ABB",
                        "001A0500113800110B0016460014870012300010DA0014EABB",
                        "001E3C00147900148500196300189E0015D700145D001830BB",
                        "00215D0018C900186D001DA9001BD600197400184B001C35BB"};
        
            auto hexastr2double = [](const QString &str, int byteCount)->QList<int>{
                QList<int> l;
                for(int i(0),j(str.size()); i < j; i+=byteCount)
                    l.append(str.mid(i,3).toInt(nullptr, 16));
                return l;
            };
        
            for(const QString & str : list)
                    qDebug() << hexastr2double(str,3);
        
        }
        
        I Offline
        I Offline
        isan
        wrote on last edited by isan
        #3

        @J-Hilk tnx , I need a QList<double>
        So I change it to :

        auto hexastr2double = [](const QString &str, int byteCount)->QList<double>{
        QList<double> l;
                   for(int i(0),j(str.size()); i < j; i+=byteCount)
                       l.append(str.mid(i,6).toInt(nullptr, 16));
                   return l;
               };
        
               for(const QString & str : fields){
                     qDebug()<<  hexastr2double(str,6);
        

        But the output is round, for example the FFD789 is equal by 16766857
        And with this code I get 1.67669e+07 =16766900
        How to prevent number rounding?

        JonBJ Christian EhrlicherC JKSHJ 3 Replies Last reply
        0
        • I isan

          @J-Hilk tnx , I need a QList<double>
          So I change it to :

          auto hexastr2double = [](const QString &str, int byteCount)->QList<double>{
          QList<double> l;
                     for(int i(0),j(str.size()); i < j; i+=byteCount)
                         l.append(str.mid(i,6).toInt(nullptr, 16));
                     return l;
                 };
          
                 for(const QString & str : fields){
                       qDebug()<<  hexastr2double(str,6);
          

          But the output is round, for example the FFD789 is equal by 16766857
          And with this code I get 1.67669e+07 =16766900
          How to prevent number rounding?

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by JonB
          #4

          @isan
          What do you mean by " prevent number rounding"? You are storing ints into doubles. So first there may not be an exact double representation of each int value, and second you seem to be commenting on the display output format of qDebug() << doubleValue, which will use the default general format, you have to specify a specific format of you don't want that.

          A separate issue is why you would want to take a hex-int-value and store it into a double in any case, you are liable to lose accuracy at least for some values, doesn't seem like a good idea....

          I 1 Reply Last reply
          3
          • I isan

            @J-Hilk tnx , I need a QList<double>
            So I change it to :

            auto hexastr2double = [](const QString &str, int byteCount)->QList<double>{
            QList<double> l;
                       for(int i(0),j(str.size()); i < j; i+=byteCount)
                           l.append(str.mid(i,6).toInt(nullptr, 16));
                       return l;
                   };
            
                   for(const QString & str : fields){
                         qDebug()<<  hexastr2double(str,6);
            

            But the output is round, for example the FFD789 is equal by 16766857
            And with this code I get 1.67669e+07 =16766900
            How to prevent number rounding?

            Christian EhrlicherC Offline
            Christian EhrlicherC Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @isan said in How to separate 3 bytes 3 bytes of QStringList:

            And with this code I get 1.67669e+07 =16766900

            Which is absolute correct.

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

            I 1 Reply Last reply
            2
            • I isan

              @J-Hilk tnx , I need a QList<double>
              So I change it to :

              auto hexastr2double = [](const QString &str, int byteCount)->QList<double>{
              QList<double> l;
                         for(int i(0),j(str.size()); i < j; i+=byteCount)
                             l.append(str.mid(i,6).toInt(nullptr, 16));
                         return l;
                     };
              
                     for(const QString & str : fields){
                           qDebug()<<  hexastr2double(str,6);
              

              But the output is round, for example the FFD789 is equal by 16766857
              And with this code I get 1.67669e+07 =16766900
              How to prevent number rounding?

              JKSHJ Offline
              JKSHJ Offline
              JKSH
              Moderators
              wrote on last edited by
              #6

              @isan said in How to separate 3 bytes 3 bytes of QStringList:

              How to prevent number rounding?

              Use int or int64_t. Don't use double.

              Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

              I 1 Reply Last reply
              0
              • Christian EhrlicherC Christian Ehrlicher

                @isan said in How to separate 3 bytes 3 bytes of QStringList:

                And with this code I get 1.67669e+07 =16766900

                Which is absolute correct.

                I Offline
                I Offline
                isan
                wrote on last edited by
                #7

                @Christian-Ehrlicher yes but rounded

                Christian EhrlicherC 1 Reply Last reply
                0
                • JKSHJ JKSH

                  @isan said in How to separate 3 bytes 3 bytes of QStringList:

                  How to prevent number rounding?

                  Use int or int64_t. Don't use double.

                  I Offline
                  I Offline
                  isan
                  wrote on last edited by
                  #8

                  @JKSH I need QList<double>

                  J.HilkJ JKSHJ 2 Replies Last reply
                  0
                  • JonBJ JonB

                    @isan
                    What do you mean by " prevent number rounding"? You are storing ints into doubles. So first there may not be an exact double representation of each int value, and second you seem to be commenting on the display output format of qDebug() << doubleValue, which will use the default general format, you have to specify a specific format of you don't want that.

                    A separate issue is why you would want to take a hex-int-value and store it into a double in any case, you are liable to lose accuracy at least for some values, doesn't seem like a good idea....

                    I Offline
                    I Offline
                    isan
                    wrote on last edited by
                    #9

                    @JonB can convert QList<int> to QList<double>?

                    JonBJ 1 Reply Last reply
                    0
                    • I isan

                      @JKSH I need QList<double>

                      J.HilkJ Offline
                      J.HilkJ Offline
                      J.Hilk
                      Moderators
                      wrote on last edited by
                      #10

                      @isan instead of relying on the printf, compare it to an const int


                      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


                      Q: What's that?
                      A: It's blue light.
                      Q: What does it do?
                      A: It turns blue.

                      1 Reply Last reply
                      0
                      • I isan

                        @Christian-Ehrlicher yes but rounded

                        Christian EhrlicherC Offline
                        Christian EhrlicherC Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by Christian Ehrlicher
                        #11

                        @isan said in How to separate 3 bytes 3 bytes of QStringList:

                        yes but rounded

                        It's just a debug output. Simply format the output correctly - C basics

                        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
                        • I isan

                          @JKSH I need QList<double>

                          JKSHJ Offline
                          JKSHJ Offline
                          JKSH
                          Moderators
                          wrote on last edited by JKSH
                          #12

                          @isan said in How to separate 3 bytes 3 bytes of QStringList:

                          I need QList<double>

                          Can you explain why?

                          Your hex strings represent integers, not floating-point numbers. I can't see why you need QList<double>.

                          I get 1.67669e+07 =16766900

                          @Christian-Ehrlicher is right. The debug output is displaying 16766857 to 6 significant digits. It is not rounding your value; the value is still 16766857.

                          You can see it by converting your value back to an integer:

                          qDebug() << int( hexastr2double(str, 6) ); // Shows 16766857
                          

                          Qt Doc Search for browsers: forum.qt.io/topic/35616/web-browser-extension-for-improved-doc-searches

                          1 Reply Last reply
                          3
                          • I isan

                            @JonB can convert QList<int> to QList<double>?

                            JonBJ Offline
                            JonBJ Offline
                            JonB
                            wrote on last edited by JonB
                            #13

                            @isan said in How to separate 3 bytes 3 bytes of QStringList:

                            @JonB can convert QList<int> to QList<double>?

                            A QList is just a list, if you can convert one int to a double you can convert a list of ints to a list of doubles.

                            You still haven't said what it is you want doubles for. Given that the input is hex representation of integers, one would have thought you would want to deal with them as ints. Without knowing your usage it's hard to be sure, but my inclination would be to convert these to a list of ints where you do not lose any precision. If you then want to use those number as doubles for some later purpose, convert the numbers to doubles at the time you need to use them as such rather than at the point you read them in.

                            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