2021-01-31 11:01 PM
Hello Everyone,
I am using STM32F767 and have generated code from STM32CubeMX. I am trying to convert a string of hexadecimal to float value, but it seems that the float is not converting the value. I am using Keil IDE UV5 ,and I have tried setting "Floating point hardware" in Target options with "Not used", "Single precision" as well as "Double precision", but nothing seems to work. Am I missing something? or is anything needed to be done so that "float" conversion works?
My code is as follows:-
char val[20];float f;
uint32_t num;
sprintf(val,"%02x%02x%02x%02x",0x43,0x71,0x3E,0xE9);
printf("val is %s\n\r",val);
sscanf(val,"%x",&num);
printf("num %x -- %d\n\r",num,num);
f = *((float*)&num);
f = ((float)num);
printf("float is %.2f\n\r",f);
This prints "float is 1131495145.00" whereas it should be "241.245743"
Help will be appreciated.
Thank you.
Solved! Go to Solution.
2021-02-01 03:25 PM
hello
f = ((float)num); takes the value of nun and assign it to f.. So is ok the 1131495145.00
comment out the " f = ((float)num); " and will take the result 241.245743
2021-02-01 03:25 PM
hello
f = ((float)num); takes the value of nun and assign it to f.. So is ok the 1131495145.00
comment out the " f = ((float)num); " and will take the result 241.245743
2021-02-01 08:24 PM
@Vangelis Fortounas Oh yes it worked !! Thank you so much....