Results 1 to 4 of 4

Thread: How to convert float to uint32 ?

  1. #1
    Join Date
    Oct 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default How to convert float to uint32 ?

    Hy,

    I'm trying to convert a float number to a uint16_t[2], according the IEEE 754 format.

    For example, I manage to do the contrary :

    Qt Code:
    1. double uint16t2double(uint16_t* buff)
    2. {
    3. uint32_t test32 = 0;
    4. test32 = buff[0];
    5. int i;
    6. double resultat;
    7.  
    8. test32 = test32 << 16;
    9.  
    10. test32 = test32 + buff[1];
    11.  
    12. char sign;
    13. signe = test32 >> 31;
    14.  
    15. unsigned char expo;
    16. expo = (test32 & 0x7F800000) >> 23;
    17. expo -= 127;
    18.  
    19. uint32_t mantissa = (test32 & 0x7FFFFF);
    20. float m = 1;
    21. for(i=1; i<=23; i++) {
    22. if((mantissa & 0x400000) != 0) {
    23. m += pow(2, -i);
    24. }
    25. mantissa = mantissa << 1;
    26. }
    27.  
    28. result = pow((double)2,(double)expo);
    29. if(sign)
    30. result = -1*result * m;
    31. else
    32. result = result * m;
    33.  
    34. return result;
    35. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone help me ?

    PS: I'm new on forum, please excuse me if my message is not in the proper section and my explanations too succinct.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to convert float to uint32 ?

    IEEE754 is the standard binary format for most architectures. If you want the 32-bit integer variable to contain bit-for-bit what a 32-bit float contains then you can use pointer trickery:
    Qt Code:
    1. #include <stdint.h>
    2. #include <iostream>
    3. #include <cassert>
    4.  
    5. int main(int argc, char **argv)
    6. {
    7. float input = 1.2345678e34;
    8.  
    9. assert(sizeof(float) == sizeof(uint32_t));
    10.  
    11. uint32_t* pInt = reinterpret_cast<uint32_t*>(&input);
    12. std::cout << *pInt << std::endl;
    13.  
    14. float* output = reinterpret_cast<float*>(pInt);
    15. std::cout << *output << std::endl;
    16.  
    17. // or via a buffer of 16 bit ints
    18. uint16_t* buff = reinterpret_cast<uint16_t*>(&input);
    19. std::cout << buff[0] << '\t' << buff[1] << std::endl;
    20.  
    21. output = reinterpret_cast<float*>(buff);
    22. std::cout << *output << std::endl;
    23.  
    24. return 0;
    25. }
    To copy to clipboard, switch view to plain text mode 
    but the result is dangerously non-portable.

  3. #3
    Join Date
    Oct 2014
    Posts
    2
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: How to convert float to uint32 ?

    I found this solution :

    Qt Code:
    1. double uint16t2double(uint16_t* buff, float data)
    2. {
    3. union FloatOrUInt
    4. {
    5. float asFloat;
    6. unsigned int asUInt;
    7. } fu;
    8.  
    9. fu.asFloat = data;
    10.  
    11. uint32_t i;
    12.  
    13. i = fu.asUInt;
    14.  
    15. buff[0] = (uint16_t)i;
    16. buff[1] = (uint16_t)(i >> 16)&0xffff;
    17. }
    To copy to clipboard, switch view to plain text mode 

    It seems to be good for me.
    And the contrary is possible too.

  4. #4

    Default Re: How to convert float to uint32 ?

    Hi
    I'm used this code.
    Qt Code:
    1. uint32_t FloatToUint(float n)
    2. {
    3. return (uint32_t)(*(uint32_t*)&n);
    4. }
    5.  
    6. float UintToFloat(uint32_t n)
    7. {
    8. return (float)(*(float*)&n);
    9. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to convert Qhorizontal_Slider int value to float!!
    By Jakr1387 in forum Qt Programming
    Replies: 2
    Last Post: 10th September 2014, 17:26
  2. Replies: 6
    Last Post: 1st February 2013, 09:32
  3. Replies: 4
    Last Post: 18th May 2011, 18:59
  4. How to convert int to float?
    By babygal in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2010, 06:06
  5. Replies: 1
    Last Post: 10th February 2009, 10:42

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.