内存的大小端是个不容忽视的问题,尤其对于多字节数据类型等。我总结了一个看内存大小端的方法。代码如下:
1:
#include "iostream"
using namespace std;
int main()
{
int a = 0x123456;
const char *p = (char *)&a;
if(*p == 0x56)
{
cout<<"little"<<endl;
}
else
{
cout<<"big edian"<<endl;
}
}
2.利用共用体实现
#include "iostream"
using namespace std;
int main()
{
union EndianType{
int a;
char b[4];
}tmpend;
tmpend.a=0x12345678;
if(tmpend.b[0] == 0x78)
{
cout<<"1 little"<<endl;
}
else
{
cout<<"2 big edian"<<endl;
}
}