This is for compile time checking
您可以使用boost头文件endian.hpp
中的信息,它涵盖了许多平台.
edit for runtime checking个
bool isLittleEndian()
{
short int number = 0x1;
char *numPtr = (char*)&number;
return (numPtr[0] == 1);
}
创建一个整数,并读取其第一个字节(最低有效字节).如果字节为1,则系统为小端,否则为大端.
edit Thinking about it个
是的,你可能会在一些平台上遇到潜在的问题(想不出有什么问题),比如sizeof(char) == sizeof(short int)
.您可以使用<stdint.h>
中提供的固定宽度多字节整数类型,或者如果您的平台没有固定宽度的多字节整数类型,您也可以根据您的使用调整Boost标头:stdint.hpp