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.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; } }
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:
From union:Code:154, 159, 4, 8, 95, 140, 4, 8
The test was done on a X86 machine.Code: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)Code:double* value = reinterpret_cast<double*>(bytes);
Both returned this value:Code:double* value = (double*)((void*)bytes);
I can't think of any solution, and neither what the problem could be.Code:4.86187e-270



LinkBack URL
About LinkBacks




CornedBee
.