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

Qt Convert Float to ByteArray

Scheduled Pinned Locked Moved Solved General and Desktop
9 Posts 5 Posters 1.9k 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.
  • N Offline
    N Offline
    neda
    wrote on last edited by
    #1

    Hi,
    I used this code for convert float to "ByteArray".
    The following codes have different results.
    Please guide me.
    Thanks

    Code 1 (It's Not True):

     float speed=20;
     float calcSpeed= funcCalcSpeed(speed);
     qDebug()<<"calcSpeed: ";
     qDebug()<<calcSpeed;
    

    Output:

    calcSpeed:
    41.6667
    
    QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float));
    qDebug()<<"byteArray: ";
    qDebug()<<byteArray;
    

    Output:

    byteArray:
    "`\xBF\x96\x02"
    

    Code 2 (It's True):

    float val=41.6667f;
    QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float));
    qDebug()<<"byteArray: ";
    qDebug()<<byteArray;
    

    Output:

    byteArray:
    "\xB3\xAA&B"
    
    kshegunovK 1 Reply Last reply
    0
    • Christian EhrlicherC Offline
      Christian EhrlicherC Offline
      Christian Ehrlicher
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @neda said in Qt Convert Float to ByteArray:

      41.6667

      Just because your float number has 6 same digits it does not mean it's exactly the same value. That's also the reason why you should not compare two floating point numbers (when they are calculated by different algorithms). See also https://en.wikipedia.org/wiki/Floating-point_arithmetic

      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
      6
      • sierdzioS Offline
        sierdzioS Offline
        sierdzio
        Moderators
        wrote on last edited by
        #3

        Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"), but QByteArray has 2 methods to help you work with floats:

        Using setNum

        const float number = 20;
        QByteArray array;
        array.setNum(number);
        

        Using number

        const float number = 20;
        const QByteArray array = QByteArray::number(number);
        

        (Z(:^

        N 1 Reply Last reply
        1
        • sierdzioS sierdzio

          Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"), but QByteArray has 2 methods to help you work with floats:

          Using setNum

          const float number = 20;
          QByteArray array;
          array.setNum(number);
          

          Using number

          const float number = 20;
          const QByteArray array = QByteArray::number(number);
          
          N Offline
          N Offline
          neda
          wrote on last edited by
          #4

          @sierdzio said in Qt Convert Float to ByteArray:

          Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"),

          Thanks for your reply.
          I want edit "Code 1" and have output "Code 2".
          Output of "code 2" is true.
          But instead of "float val=41.6667f;" I have "funcCalcSpeed(speed)".

          Code 2 (It's True):

          float val=41.6667f;
          QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float));
          qDebug()<<"byteArray: ";
          qDebug()<<byteArray;
          

          Output:

          byteArray:
          "\xB3\xAA&B"
          

          I tried"setNum" and "number", but they did not have the desired output (Like output of code 2).

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

            I still don't get your problem... did you understand what I wrote above?

            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
            3
            • N neda

              @sierdzio said in Qt Convert Float to ByteArray:

              Not sure what you are trying to achieve (hint: when posting a thread, also wrinte some question, or at least some "expected result"),

              Thanks for your reply.
              I want edit "Code 1" and have output "Code 2".
              Output of "code 2" is true.
              But instead of "float val=41.6667f;" I have "funcCalcSpeed(speed)".

              Code 2 (It's True):

              float val=41.6667f;
              QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float));
              qDebug()<<"byteArray: ";
              qDebug()<<byteArray;
              

              Output:

              byteArray:
              "\xB3\xAA&B"
              

              I tried"setNum" and "number", but they did not have the desired output (Like output of code 2).

              aha_1980A Offline
              aha_1980A Offline
              aha_1980
              Lifetime Qt Champion
              wrote on last edited by aha_1980
              #6
              This post is deleted!
              1 Reply Last reply
              0
              • sierdzioS Offline
                sierdzioS Offline
                sierdzio
                Moderators
                wrote on last edited by
                #7

                What prevents you from combining both approaches?

                const float speed=20;
                coinst float calcSpeed= funcCalcSpeed(speed);
                const QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float));
                 qDebug()<<"calcSpeed: ";
                 qDebug()<<byteArray;
                

                (yeah I think I still don't fully get what you're trying to achieve).

                (Z(:^

                1 Reply Last reply
                1
                • N neda

                  Hi,
                  I used this code for convert float to "ByteArray".
                  The following codes have different results.
                  Please guide me.
                  Thanks

                  Code 1 (It's Not True):

                   float speed=20;
                   float calcSpeed= funcCalcSpeed(speed);
                   qDebug()<<"calcSpeed: ";
                   qDebug()<<calcSpeed;
                  

                  Output:

                  calcSpeed:
                  41.6667
                  
                  QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&calcSpeed), sizeof(float));
                  qDebug()<<"byteArray: ";
                  qDebug()<<byteArray;
                  

                  Output:

                  byteArray:
                  "`\xBF\x96\x02"
                  

                  Code 2 (It's True):

                  float val=41.6667f;
                  QByteArray byteArray = QByteArray::fromRawData(reinterpret_cast<char *>(&val), sizeof(float));
                  qDebug()<<"byteArray: ";
                  qDebug()<<byteArray;
                  

                  Output:

                  byteArray:
                  "\xB3\xAA&B"
                  
                  kshegunovK Offline
                  kshegunovK Offline
                  kshegunov
                  Moderators
                  wrote on last edited by
                  #8

                  Your problem is not converting the float to a sequence of bytes, but that you assume (erroneously) that 2 floating points are the same if they match up to the 4th decimal digit. You need to read and understand what floating points are and why this assumption can almost never be fulfilled.

                  (See @Christian-Ehrlicher's link with the wiki article)

                  Read and abide by the Qt Code of Conduct

                  1 Reply Last reply
                  4
                  • N Offline
                    N Offline
                    neda
                    wrote on last edited by
                    #9

                    @Christian-Ehrlicher
                    @sierdzio
                    @kshegunov

                    Thanks a lot
                    I understood what is happened and my problem has solved.

                    1 Reply Last reply
                    1

                    • Login

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