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
Forum Updated to NodeBB v4.3 + New Features

How to separate 3 bytes 3 bytes of QStringList

Scheduled Pinned Locked Moved Unsolved General and Desktop
13 Posts 5 Posters 845 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.
  • 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