问题
I'm writing and reading binary data (std::ios::binary
) in C++ using std::fstream
- this includes integer and floating point values. While my code works on my own architecture, I wan't to make sure, that it's portable and i.e. binary files written on my machine shall still be read properly of an machine with different endianness. So my idea was that I will add in the binary file at first byte a value which will indicate the endianness of the file.
As there is no guarantee, that endianness of integer and floating points are the same, I need to get the information for both datatypes separately. While getting the endianess for integer was rather simple with pointer arithmetic, I'm lost how to get the endianess for float at runtime. Any idea?
My code looks like:
#include <cstdint>
#define INT_LITTLE_ENDIAN 0x01u
#define INT_BIG_ENDIAN 0x02u
#define FLOAT_LITTLE_ENDIAN 0x04u
#define FLOAT_BIG_ENDIAN 0x08u
uint8_t getEndianess(){
uint8_t endianess = 0x00;
uint16_t integerNumber = 0x1;
uint8_t *numPtr = (uint8_t*)&integerNumber;
if (numPtr[0] == 1) {
endianess |= INT_LITTLE_ENDIAN;
}else {
endianess |= INT_BIG_ENDIAN;
}
/* TODO: check endianess for float */
return endianess;
}
回答1:
Well, besides just endianness, you also have the potential for non-IEEE-754 formats, but that is admittedly rare.
If you can assume IEEE-754 binary, then it almost certainly uses the same endianness as integers, but you can readily check by using a floating point value that is a negative power of two (such as -1.0), which will have a non-zero MSbyte (containing the sign and part of the exponent) and a zero LSbyte (containing the least significant mantissa bits).
float floatNumber = -1.0;
uint8_t *numPtr = (uint8_t*)&floatNumber;
if (numPtr[0] == 0) {
endianess |= FLOAT_LITTLE_ENDIAN;
} else {
endianess |= FLOAT_BIG_ENDIAN;
}
回答2:
If we assume floats have sign as the topmost bit (like IEEE) and they aren't, for example, like two's complement, you can easily make a number, negate it and check if the first or the last byte changed.
来源:https://stackoverflow.com/questions/35763790/endianness-for-floating-point