Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

How to separate 3 bytes 3 bytes of QStringList



  • 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?


  • Moderators

    @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);
    
    }
    


  • @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?



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


  • Qt Champions 2019

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


  • Moderators

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



  • @Christian-Ehrlicher yes but rounded



  • @JKSH I need QList<double>



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


  • Moderators

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


  • Qt Champions 2019

    @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


  • Moderators

    @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
    


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


Log in to reply