Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Mobile and Embedded
  4. 4 bytes array into float
Forum Updated to NodeBB v4.3 + New Features

4 bytes array into float

Scheduled Pinned Locked Moved Solved Mobile and Embedded
6 Posts 4 Posters 3.2k 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.
  • S Offline
    S Offline
    sandycoolxyz
    wrote on last edited by
    #1

    Hello,
    I am using qt 4.8.5.
    I have an array of 4 elements of _u8 type.
    I would like to convert it into float. But I end up in wrong value.

    = {1, 0, 0, 0} (hex values are 01 00 00 00)

    qint32 value;
    float value2;
    for(i = 0; i < 4; i++)
          {
            value = (value << 8) + (data[i] & 0xff);                // if MSB is bit number 1
          }
    value2 = (float)value;
    
    

    If I out the value as int, the value I get is 16777216.
    If I out the value2 as float, I get 1.67772e07. But I am expecting a float value 2.35099e-38(https://gregstoll.dyndns.org/~gregstoll/floattohex/).
    How can I convert the 4 byte array into float?

    jsulmJ 1 Reply Last reply
    0
    • S sandycoolxyz

      Hello,
      I am using qt 4.8.5.
      I have an array of 4 elements of _u8 type.
      I would like to convert it into float. But I end up in wrong value.

      = {1, 0, 0, 0} (hex values are 01 00 00 00)

      qint32 value;
      float value2;
      for(i = 0; i < 4; i++)
            {
              value = (value << 8) + (data[i] & 0xff);                // if MSB is bit number 1
            }
      value2 = (float)value;
      
      

      If I out the value as int, the value I get is 16777216.
      If I out the value2 as float, I get 1.67772e07. But I am expecting a float value 2.35099e-38(https://gregstoll.dyndns.org/~gregstoll/floattohex/).
      How can I convert the 4 byte array into float?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by jsulm
      #2

      @sandycoolxyz Shouldn't it be like:

      qint32 value = 0;
      for(i = 0; i < 4; i++)
      {
           value |= data[i]<<(8*i);
          // or (depending on byte order in the array
          value |= data[i]<<((4-i)*8);
      }
      

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      2
      • Paul ColbyP Offline
        Paul ColbyP Offline
        Paul Colby
        wrote on last edited by
        #3

        One option, assuming your array is always big-endian, and your code has to support both big an little-endian platforms:

            quint8 a[] = {1, 0, 0, 0};
            quint32 localEndian = qFromBigEndian<quint32>(a);
            qDebug() << *reinterpret_cast<float *>(&localEndian);
        

        Output:

        2.35099e-38
        

        Cheers.

        1 Reply Last reply
        4
        • S Offline
          S Offline
          sandycoolxyz
          wrote on last edited by
          #4

          Thanks Paul Colby and jsulm for the reply.
          @jsulm I believe Its the same code as mine, just using addition instead of OR.

          @Paul-Colby Thanks for the solution.

          Cheers.

          M 1 Reply Last reply
          1
          • S sandycoolxyz

            Thanks Paul Colby and jsulm for the reply.
            @jsulm I believe Its the same code as mine, just using addition instead of OR.

            @Paul-Colby Thanks for the solution.

            Cheers.

            M Offline
            M Offline
            mvuori
            wrote on last edited by
            #5

            Problems like that are exactly why people like to avoid bitwise operation when possible... For example, in this case many would prefer using an union of float & array for the conversion as it is more readable and maintainable years later...

            jsulmJ 1 Reply Last reply
            0
            • M mvuori

              Problems like that are exactly why people like to avoid bitwise operation when possible... For example, in this case many would prefer using an union of float & array for the conversion as it is more readable and maintainable years later...

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by
              #6

              @mvuori But the union would not solve the problem with endianness.

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              2

              • Login

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