I have adapted the above code in to this:
The check for endiannes works on both platforms. I get false on X86 and true on PPC.
short check = 0xAABB;
uint8_t* endianCheck = reinterpret_cast<uint8_t*>(&check);
return (endianCheck == 0xAA && endianCheck == 0xBB);
uint8_t* convertDoubleToBytes(double value)
uint8_t* bytes = reinterpret_cast<uint8_t*>(&value);
uint8_t* swappedBytes = new uint8_t[sizeof(double)];
for (int i = 0, j = 7; i <= 7 && j >= 0; i++, j--)
swappedBytes[i] = bytes[j];
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.
154, 159, 4, 8, 95, 140, 4, 8
The test was done on a X86 machine.
154, 153, 153, 153, 153, 25, 73, 64
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:
and (probably very wrong)
double* value = reinterpret_cast<double*>(bytes);
Both returned this value:
double* value = (double*)((void*)bytes);
I can't think of any solution, and neither what the problem could be.