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. Converting Qbyte array to array and to double
Forum Updated to NodeBB v4.3 + New Features

Converting Qbyte array to array and to double

Scheduled Pinned Locked Moved Unsolved General and Desktop
14 Posts 3 Posters 833 Views 2 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.
  • D Offline
    D Offline
    dziko147
    wrote on 30 Jul 2021, 13:43 last edited by dziko147
    #1

    Hello , I get some data from a CAN Bus . So my data is some float and some double .

    What I did is :

    float myData;
    double myDatadouble;
    
                       QDataStream stream(data);
                        qDebug() << data.size() << " Data Data&size" ;
                       
                        if (data.size()== 5){
                           quint8 byte;
                            stream >> byte; // skip first byte
                  stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
                            stream >> myData; // read data
                            qDebug()  << "this is data"<< myData ;
                            QString DatatoString =QString::number(myData);
                            Outputs[i]=DatatoString;
                        qDebug() << "In outputs data" << Outputs[i];
                        }
    else {
    
                       quint8 byte;
                        stream >> byte; // skip first byte
                            stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
                         stream >> myDatadouble; // read data
                        qDebug()  << "this is data"<< myDatadouble;
                        QString DatatoString =QString::number(myDatadouble);
                        Outputs[i]=DatatoString;
                        qDebug() << "In outputs data" << Outputs[i];
                        }              
                       
    
    

    I get only float converted , the double is usually "0" .

    thankyou

    A C 2 Replies Last reply 30 Jul 2021, 13:58
    0
    • D dziko147
      30 Jul 2021, 13:43

      Hello , I get some data from a CAN Bus . So my data is some float and some double .

      What I did is :

      float myData;
      double myDatadouble;
      
                         QDataStream stream(data);
                          qDebug() << data.size() << " Data Data&size" ;
                         
                          if (data.size()== 5){
                             quint8 byte;
                              stream >> byte; // skip first byte
                    stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
                              stream >> myData; // read data
                              qDebug()  << "this is data"<< myData ;
                              QString DatatoString =QString::number(myData);
                              Outputs[i]=DatatoString;
                          qDebug() << "In outputs data" << Outputs[i];
                          }
      else {
      
                         quint8 byte;
                          stream >> byte; // skip first byte
                              stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
                           stream >> myDatadouble; // read data
                          qDebug()  << "this is data"<< myDatadouble;
                          QString DatatoString =QString::number(myDatadouble);
                          Outputs[i]=DatatoString;
                          qDebug() << "In outputs data" << Outputs[i];
                          }              
                         
      
      

      I get only float converted , the double is usually "0" .

      thankyou

      A Offline
      A Offline
      artwaw
      wrote on 30 Jul 2021, 13:58 last edited by
      #2

      @dziko147 in the else part of the case you do QString::number on the float instead of the double?

      Plus, using QString::number you can control precision on the double too https://doc.qt.io/qt-5/qstring.html#number-6

      For more information please re-read.

      Kind Regards,
      Artur

      1 Reply Last reply
      0
      • D Offline
        D Offline
        dziko147
        wrote on 30 Jul 2021, 14:08 last edited by
        #3

        @artwaw thanks for your reply . Ok I will remove the Qstring .

        Also I get unreadable data like " 3.13891e-43 " for a array "0xff000000e0" .

        I use a online converter and give the same value .

        But there is a way to make it readable .

        thank you

        A 1 Reply Last reply 30 Jul 2021, 14:20
        0
        • D dziko147
          30 Jul 2021, 14:08

          @artwaw thanks for your reply . Ok I will remove the Qstring .

          Also I get unreadable data like " 3.13891e-43 " for a array "0xff000000e0" .

          I use a online converter and give the same value .

          But there is a way to make it readable .

          thank you

          A Offline
          A Offline
          artwaw
          wrote on 30 Jul 2021, 14:20 last edited by
          #4

          @dziko147
          again: in the "else" part you have

          qDebug()  << "this is data"<< myDatadouble;
          QString DatatoString =QString::number(myData);
          

          where I think you should have

          qDebug()  << "this is data"<< myDatadouble;
          QString DatatoString =QString::number(myDatadouble);
          

          since you do not even set myData in that part. Hence your string contains rubbish, you don't even initialise the variable.

          For more information please re-read.

          Kind Regards,
          Artur

          1 Reply Last reply
          0
          • D Offline
            D Offline
            dziko147
            wrote on 30 Jul 2021, 14:25 last edited by
            #5

            @artwaw yes in my code I have

            qDebug()  << "this is data"<< myDatadouble;
            QString DatatoString =QString::number(myDatadouble);
            

            just when i copying it i I was mistaken .

            I update my topic :)

            1 Reply Last reply
            0
            • D dziko147
              30 Jul 2021, 13:43

              Hello , I get some data from a CAN Bus . So my data is some float and some double .

              What I did is :

              float myData;
              double myDatadouble;
              
                                 QDataStream stream(data);
                                  qDebug() << data.size() << " Data Data&size" ;
                                 
                                  if (data.size()== 5){
                                     quint8 byte;
                                      stream >> byte; // skip first byte
                            stream.setFloatingPointPrecision(QDataStream::SinglePrecision);
                                      stream >> myData; // read data
                                      qDebug()  << "this is data"<< myData ;
                                      QString DatatoString =QString::number(myData);
                                      Outputs[i]=DatatoString;
                                  qDebug() << "In outputs data" << Outputs[i];
                                  }
              else {
              
                                 quint8 byte;
                                  stream >> byte; // skip first byte
                                      stream.setFloatingPointPrecision(QDataStream::DoublePrecision);
                                   stream >> myDatadouble; // read data
                                  qDebug()  << "this is data"<< myDatadouble;
                                  QString DatatoString =QString::number(myDatadouble);
                                  Outputs[i]=DatatoString;
                                  qDebug() << "In outputs data" << Outputs[i];
                                  }              
                                 
              
              

              I get only float converted , the double is usually "0" .

              thankyou

              C Offline
              C Offline
              CP71
              wrote on 30 Jul 2021, 14:30 last edited by CP71
              #6

              @dziko147
              Hi,
              you have checked only if (data.size()== 5) in first if,
              Did you check in else that the data size is correct?
              I think no

              1 Reply Last reply
              0
              • D Offline
                D Offline
                dziko147
                wrote on 30 Jul 2021, 14:33 last edited by
                #7

                @CP71 yes it return a size :8 byte

                C 1 Reply Last reply 30 Jul 2021, 14:35
                0
                • D dziko147
                  30 Jul 2021, 14:33

                  @CP71 yes it return a size :8 byte

                  C Offline
                  C Offline
                  CP71
                  wrote on 30 Jul 2021, 14:35 last edited by
                  #8

                  @dziko147
                  8! so doesn't you have a one spare byte in else as in float case?

                  1 Reply Last reply
                  0
                  • D Offline
                    D Offline
                    dziko147
                    wrote on 30 Jul 2021, 14:38 last edited by dziko147
                    #9

                    @CP71 said in Converting Qbyte array to array and to double:

                    8! so doesn't you have a one spare byte in else as in float case?

                    no I usually get frame started with FF .

                    I mean than in the case of a float I get "FF + 4Byte data"

                    In the case of a Double I get "FF +7Byte data "

                    and here I skip the first byte

                                       quint8 byte;
                                        stream >> byte; // skip first byte
                    
                    C 1 Reply Last reply 30 Jul 2021, 14:41
                    0
                    • D dziko147
                      30 Jul 2021, 14:38

                      @CP71 said in Converting Qbyte array to array and to double:

                      8! so doesn't you have a one spare byte in else as in float case?

                      no I usually get frame started with FF .

                      I mean than in the case of a float I get "FF + 4Byte data"

                      In the case of a Double I get "FF +7Byte data "

                      and here I skip the first byte

                                         quint8 byte;
                                          stream >> byte; // skip first byte
                      
                      C Offline
                      C Offline
                      CP71
                      wrote on 30 Jul 2021, 14:41 last edited by CP71
                      #10

                      @dziko147 said in Converting Qbyte array to array and to double:

                      In the case of a Double I get "FF +7Byte data "

                      Sorry, but in double case I think you'd send 0xFF+8bytes, anyway I think your application tries read a double at 8 bytes

                      1 Reply Last reply
                      0
                      • D Offline
                        D Offline
                        dziko147
                        wrote on 30 Jul 2021, 15:00 last edited by
                        #11

                        @CP71 you are right :D

                        1 Reply Last reply
                        0
                        • D Offline
                          D Offline
                          dziko147
                          wrote on 30 Jul 2021, 15:01 last edited by
                          #12

                          @CP71 now how can make it readable .
                          For example I get " 3.13891e-43 " for a array "0xff000000e0" .
                          please

                          C 1 Reply Last reply 30 Jul 2021, 15:15
                          0
                          • D dziko147
                            30 Jul 2021, 15:01

                            @CP71 now how can make it readable .
                            For example I get " 3.13891e-43 " for a array "0xff000000e0" .
                            please

                            C Offline
                            C Offline
                            CP71
                            wrote on 30 Jul 2021, 15:15 last edited by
                            #13

                            @dziko147
                            It is wrong!

                            https://www.binaryconvert.com/convert_double.html

                            As last time, I must go.
                            Sorry, bye

                            1 Reply Last reply
                            0
                            • D Offline
                              D Offline
                              dziko147
                              wrote on 30 Jul 2021, 17:01 last edited by
                              #14

                              @CP71 as u like bro good bye

                              1 Reply Last reply
                              1

                              4/14

                              30 Jul 2021, 14:20

                              10 unread
                              • Login

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