I have adapted the above code in to this:
Code:
bool isOSBigEndian()
{
short check = 0xAABB;
uint8_t* endianCheck = reinterpret_cast<uint8_t*>(&check);
return (endianCheck[0] == 0xAA && endianCheck[1] == 0xBB);
}
uint8_t* convertDoubleToBytes(double value)
{
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
if (isOSBigEndian())
{
uint8_t* swappedBytes = new uint8_t[sizeof(double)];
for (int i = 0, j = 7; i <= 7 && j >= 0; i++, j--)
{
swappedBytes[i] = bytes[j];
}
return swappedBytes;
}
else
{
return bytes;
}
}
The check for endiannes works on both platforms. I get false on X86 and true on PPC.
But the bytes i get from the convertDoubleToBytes function are like garbage when compared to those from an union. The numbers don't change between different executions of the program.
From function:
Code:
154, 159, 4, 8, 95, 140, 4, 8
From union:
Code:
154, 153, 153, 153, 153, 25, 73, 64
The test was done on a X86 machine.
I thought maybe the function was correct in some sort of odd way, so i tried to convert it back to a double using the following:
Code:
double* value = reinterpret_cast<double*>(bytes);
and (probably very wrong)
Code:
double* value = (double*)((void*)bytes);
Both returned this value:
I can't think of any solution, and neither what the problem could be.