I am trying to convert a "double" value (say 1.12345) to 8 byte hex string. I am using the following function to convert double value to hex string.
std::string double_to_hex_string(double d)
{
unsigned char *buffer = (unsigned char*)&d;
const int bufferSize = sizeof(double);
char converted[bufferSize * 2 + 1];
//char converted[bufferSize];
int j = 0;
for(int i = 0 ; i < bufferSize ; ++i)
{
sprintf(&converted[j*2], "%02X", buffer[i]);
++j;
}
string hex_string(converted);
return hex_string;
}
This function returns the 16 byte hex string. I then compress this string to fit into 8 bytes through this code
string hexStr = double_to_hex_string(TempD);
unsigned char sample[8];
for ( int i = 0; i < hexStr.length() / 2 ; i++)
{
sscanf( (hexStr.substr(i*2,2)).c_str(), "%02X", &sample[i]);
}
Now, how can I get the hex digits representing these 8 bytes in "sample" array. There should be only one hex digit per byte. I need to append this 8 byte hex string to a global string.
If there is any other solution which can convert a double value to 8 hex digits and vice versa, that would be highly appreciated.
Regards.