# Thread: How to convert float to uint32 ?

1. LD
Beginner
Join Date
Oct 2014
Posts
2
Qt products
Platforms

## 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:
`double uint16t2double(uint16_t* buff){    uint32_t test32 = 0;    test32 = buff[0];    int i;    double resultat;     test32 = test32 << 16;     test32 = test32 + buff[1];     char sign;    signe = test32 >> 31;     unsigned char expo;    expo = (test32 & 0x7F800000) >> 23;    expo -= 127;     uint32_t mantissa = (test32 & 0x7FFFFF);    float m = 1;    for(i=1; i<=23; i++) {        if((mantissa & 0x400000) != 0) {            m += pow(2, -i);        }        mantissa = mantissa << 1;    }     result = pow((double)2,(double)expo);    if(sign)        result = -1*result * m;    else        result = result * m;     return result;}`
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. Guru
Join Date
Mar 2009
Location
Brisbane, Australia
Posts
7,671
Thanks
13
Thanked 1,595 Times in 1,523 Posts
Qt products
Platforms
Wiki edits
17

## 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:
`#include <stdint.h>#include <iostream>#include <cassert> int main(int argc, char **argv){    float input = 1.2345678e34;     assert(sizeof(float) == sizeof(uint32_t));     uint32_t* pInt = reinterpret_cast<uint32_t*>(&input);    std::cout << *pInt << std::endl;     float* output = reinterpret_cast<float*>(pInt);    std::cout << *output << std::endl;     // or via a buffer of 16 bit ints    uint16_t* buff = reinterpret_cast<uint16_t*>(&input);    std::cout << buff[0] << '\t' << buff[1] << std::endl;     output = reinterpret_cast<float*>(buff);    std::cout << *output << std::endl;     return 0;}`
To copy to clipboard, switch view to plain text mode
but the result is dangerously non-portable.

3. LD
Beginner
Join Date
Oct 2014
Posts
2
Qt products
Platforms

## Re: How to convert float to uint32 ?

I found this solution :

Qt Code:
`double uint16t2double(uint16_t* buff, float data){    union FloatOrUInt    {        float asFloat;        unsigned int asUInt;    } fu;     fu.asFloat = data;     uint32_t i;     i = fu.asUInt;     buff[0] = (uint16_t)i;    buff[1] = (uint16_t)(i >> 16)&0xffff;}`
To copy to clipboard, switch view to plain text mode

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

4. Beginner
Join Date
Aug 2016
Posts
1

## Re: How to convert float to uint32 ?

Hi
I'm used this code.
Qt Code:
`uint32_t FloatToUint(float n){   return (uint32_t)(*(uint32_t*)&n);} float UintToFloat(uint32_t n){   return (float)(*(float*)&n);}`
To copy to clipboard, switch view to plain text mode