I have a BIG problem with the answer to this question Swap bits in c++ for a double
Yet, this question is more or less what I search for: I receive a double from th
I could not make John Källén code work on my machine. Moreover, it might be more useful to convert the double into bytes (8 bit, 1 char):
template
string to_byte_string(const T& v)
{
char* begin_ = reinterpret_cast(v);
return string(begin_, begin_ + sizeof(T));
}
template
T from_byte_string(std::string& s)
{
assert(s.size() == sizeof(T) && "Wrong Type Cast");
return *(reinterpret_cast(&s[0]));
}
This code will also works for structs which are using POD types.
If you really want the double as two ints
double d;
int* data = reinterpret_cast(&d);
int first = data[0];
int second = data[1];
Finally, long int will not always be a 64bit integer (I had to use long long int to make a 64bit int on my machine).