I am currently using this way to cast a double or float to an array of bytes (uint8_t) and back:
To bytes:
Code:
double data = 5.5;
std::queue<uint8_t> ndata;
uint8_t* tmp = (uint8_t*)&data;
for (int i = 0; i < sizeof(double); i++)
ndata.push(tmp[i]);
return ndata;
And back:
Code:
double data = 0.0;
std::queue<uint8_t> ndata;
uint8_t* tmp = (uint8_t*)&data;
for (int i = 0; i < sizeof(double); i++)
tmp[i] = ndata.front(), ndata.pop();
return ndata;
This is butt ugly code though, is there any other way to do this properly in a C++ way (or with Boost)? The resulting bytes will be eventually sent over a network connection (loopback interface actually).
I am working with GCC on Linux x64.