The following functions create a hex float/double string:
Code: Select all
std::string HexFloat(float val)
{
char hex[128]; // Probably don't need such a large char array.
std::sprintf(hex, "%a", val);
return hex;
}
std::string HexDouble(double val)
{
char hex[128]; // Probably don't need such a large char array.
std::sprintf(hex, "%a", val);
return hex;
}
Code: Select all
float HexFloat(std::string str)
{
float val;
std::sscanf(str.c_str(), "%a", &val);
return val;
}
double HexDouble(std::string str)
{
double val;
std::sscanf(str.c_str(), "%a", &val);
return val;
}
Code: Select all
float HexFloat(std::string str)
{
return std::strtof(str.c_str(), 0);
}
double HexDouble(std::string str)
{
return std::strtod(str.c_str(), 0);
}
Even if I get this to work, there's no guarantee that it'll work for other platforms/compilers.
There's another method talked about, that uses memcpy into an int.
std::hexfloat seems to be something to do with streams, so I can't see how to use it here.
Any suggestions will be gratefully received :)
[Edit] This might have to be coded from first principles (and method will be dependent on how float/double is stored in bits).