I have a BIG problem with the answer to this question Change bit in C ++ to double
However, this question is more or less what I am looking for: I get double from the network, and I want to encode it correctly on my machine.
In the case when I get int , I execute this code with ntohl :
int * piData = reinterpret_cast<int*>((void*)pData);
But in the case when I get double , I do not know what to do.
The answer to the question involves:
template <typename T> void swap_endian(T& pX) { char& raw = reinterpret_cast<char&>(pX); std::reverse(&raw, &raw + sizeof(T)); }
However, if I bring this site :
The ntohl() function converts the unsigned integer netlong from network byte order to host byte order. When the two byte orders are different, this means the endian-ness of the data will be changed. When the two byte orders are the same, the data will not be changed.
In contrast, @GManNickG the answer to the question always does the inverse with std::reverse .
Am I really wrong, given that this answer is wrong? (as part of network management endianess, the use of which ntohl suggests, although it was not explicitly indicated in the OP question header).
In the end: should I split my double into two parts of 4 bytes and apply the ntohl function in two parts? Are there any other canonical solutions?
Also this interesting question in C is hosted on a double network? but it is limited to 32-bit values. And the answer says doubles should be converted to strings due to differences in architecture. I am also going to work with sound samples, should I really consider converting all samples to strings in my database? (doubles come from the database I'm querying over the network)