T
T
trenrnd2017-01-26 16:12:39
Arduino
trenrnd, 2017-01-26 16:12:39

How to do hex to float conversion correctly?

Good day. I am studying programming MK STM32F103, a problem arose. Why does this conversion error occur?
float Variable;
Variable = 0x3EAE147B; // in decimal - 0.340
Immediately after this operation, the debugger writes that Variable is equal to 0x4E7AB852, so the number is completely different (1.015).
Thanks for the help!

Answer the question

In order to leave comments, you need to log in

2 answer(s)
M
Mercury13, 2017-01-26
@Mercury13

We've given float an integer. Union must be used.

union {
        float asF;
        uint32_t asI;
    };
    asF = 0x3EAE147B; // в десятичной - 0.340
    std::cout << asF << ' ' << std::hex << asI << std::endl;
    asI = 0x3EAE147B;
    std::cout << asF << ' ' << std::hex << asI << std::endl;
    return 0;

O
one_day, 2017-08-14
@IG51

www.php.su/round()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question