Click here to Skip to main content
15,892,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a Hex String = "41BD9A69", I need to convert this hex string into ASCII Hex IEEE Float.
I meant how do we convert this hex string into equivalent float value of 23.700396 programatically in C++ or using library.
Hex String = "41BD9A69" = 23.700396
Posted

You might use the union trick:
C++
#include <iostream>
#include <sstream>
#include <iomanip>
using namespace std;

union ulf
{
    unsigned long ul;
    float f;
};

int main()
{
    ulf u;
    string str = "41BD9A69";
    stringstream ss(str);
    ss >> hex >> u.ul;
    float f = u.f;
    cout << f << endl;
}
 
Share this answer
 
Comments
Member 13995944 5-Oct-18 1:42am    
I want half precision float value, My string is "00bff409", converted float vlaue is not suitable for arithmetic calculation.
CPallini 5-Oct-18 2:42am    
Half precision float value, per my understanding, is a 16 bit number. You have a 24 bit one.
It's not complex, but look here: http://gregstoll.dyndns.org/~gregstoll/floattohex/[^] - it's a calculator module, that includes C source to do just that.
 
Share this answer
 
This should solve your problem

C#
float f;
long l;

l = strtol(HEXString, (char**)NULL, 16);
f = (float)l;

printf("%f", f);


See below for detail explanation

Convert a hexadecimal to a float and viceversa in c[^]
 
Share this answer
 
Comments
CPallini 6-Jun-14 5:47am    
Did you try your code?

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900