Search code examples
c++11castingfloating-pointc++14uint16

How to cast uint16_t into a float in C++


I am running C++14 on MacOS High Sierra.

I have an uint16_t returned from a method and the value can range from 100 to like 8000 odd.

I want to convert it to a float. So, if it is 289 then the float should be 289.0. I am trying all different ways to cast the uint16_t but it my float variable always gets zeroes.

uint16_t i_value = 289;

Tried this:

float f_value = static_cast(i_value);

And tried this:

float f_value = (float)i_value;

But nothing works.

Question:
How can I cast uint16_t into a float?


Solution

  • It is an implicit conversion (both ways), no cast is required:

    uint16_t i_value = 289;
    float f = i_value;