Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    Solved 4 bytes array into float

    Mobile and Embedded
    4
    6
    2496
    Loading More Posts
    • 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
      sandycoolxyz last edited by

      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?

      jsulm 1 Reply Last reply Reply Quote 0
      • jsulm
        jsulm Lifetime Qt Champion @sandycoolxyz last edited by jsulm

        @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 Reply Quote 2
        • P
          Paul Colby last edited by

          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 Reply Quote 4
          • S
            sandycoolxyz last edited by

            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 Reply Quote 1
            • M
              mvuori @sandycoolxyz last edited by

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

              jsulm 1 Reply Last reply Reply Quote 0
              • jsulm
                jsulm Lifetime Qt Champion @mvuori last edited by

                @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 Reply Quote 2
                • First post
                  Last post