The 'byte order' in which it is sent from the network is important. (Big or Little endian Endianness)
Hopefully, the example below may help you find a solution to your issue.
// CppConsoleApplication1.cpp
//
#include <stdio.h>
#include <tchar.h>
#include <stdint.h>
#include <assert.h>
#include <vector>
#define MAX_DOUBLE 1.7976931348623157E+308
#define DOUBLE_SIZE ((int)sizeof(double))
int GetPlatformByteOrder(void);
const int LITTLE_ENDIAN = 1234;
const int BIG_ENDIAN = 4321;
const int BYTE_ORDER = GetPlatformByteOrder();
int GetPlatformByteOrder(void)
{
const unsigned int cbuffer = sizeof(unsigned int);
unsigned int value = 0x0A0B0C0D;
unsigned char buffer[cbuffer];
memcpy_s((void*)buffer, cbuffer, (const void*)&value, cbuffer);
return (buffer[0] == 0x0A) ? BIG_ENDIAN : LITTLE_ENDIAN;
}
double ToDouble(const std::vector<unsigned char>& from, uint32_t startIndex, int byteOrder = BYTE_ORDER)
{
assert((from.size() - startIndex) >= DOUBLE_SIZE);
double retVal = 0;
unsigned char* p = (unsigned char*)&retVal;
if (byteOrder != BYTE_ORDER) {
for (int32_t i = 0; i < DOUBLE_SIZE; i++) {
p[i] = from[(DOUBLE_SIZE - (i + 1)) + startIndex];
}
}
else {
for (int32_t i = 0; i < DOUBLE_SIZE; i++) {
p[i] = from[i + startIndex];
}
}
return retVal;
}
std::vector<unsigned char> ToArray(double value, int byteOrder = BYTE_ORDER)
{
std::vector<unsigned char> retVal;
unsigned char* p = (unsigned char*)&value;
if (byteOrder != BYTE_ORDER) {
for (int32_t i = 0; i < (int32_t)DOUBLE_SIZE; i++) {
retVal.push_back(p[DOUBLE_SIZE - (i + 1)]);
}
}
else {
for (int32_t i = 0; i < (int32_t)DOUBLE_SIZE; i++) {
retVal.push_back(p[i]);
}
}
return retVal;
}
int _tmain(int argc, _TCHAR* argv[])
{
double value0 = MAX_DOUBLE;
double value1;
std::vector<unsigned char> datas;
// Test1
datas = ToArray(value0);
value1 = ToDouble(datas, 0);
printf("Before: %1.16E\n", value0);
printf("After : %1.16E\n", value1);
printf("Result: %s\n", value1 == value0 ? "Correct" : "Incorrect");
printf("\n");
//assert(value1 == value0);
// Test2
datas = ToArray(value0, BIG_ENDIAN);
value1 = ToDouble(datas, 0, BIG_ENDIAN);
printf("Before: %1.16E\n", value0);
printf("After : %1.16E\n", value1);
printf("Result: %s\n", value1 == value0 ? "Correct" : "Incorrect");
printf("\n");
//assert(value1 == value0);
// Test3 (with error)
// For example, We don't know the byte order of the source.
datas = ToArray(value0, BIG_ENDIAN);
// For example, we assume the byte order of the source is LITTLE_ENDIAN.
value1 = ToDouble(datas, 0, LITTLE_ENDIAN); // ???
printf("Before: %1.16E\n", value0);
printf("After : %1.16E\n", value1);
printf("Result: %s\n", value1 == value0 ? "Correct" : "Incorrect");
printf("\n");
//assert(value1 == value0);
printf("\nPress enter to exit.");
getchar();
return 0;
}
The result will be as shown below.
Before: 1.7976931348623157E+308
After : 1.7976931348623157E+308
Result: Correct
Before: 1.7976931348623157E+308
After : 1.7976931348623157E+308
Result: Correct
Before: 1.7976931348623157E+308
After : -1.#QNAN00000000000E+000
Result: Incorrect