大端模式:指数据的低位保存在内存的高地址中,而数据的高位保存在内存的低地址中
小端模式:指数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中
例如int型整数 0x12345678
方法一
#include <iostream>
#include <iomanip>
using namespace std;
union Endian
{
int n;
char c;
};
int main()
{
Endian e;
cin >> hex >> e.n;
char low = e.n & 0x000000ff;
if(e.c == low)
{
cout << "little endian!" << endl;
}
else
{
cout << "big endian!" << endl;
}
return 0;
}
测试
DEV测试
VS测试
Ubuntu16.04测试
方法二
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int n;
cin >> hex >> n;
char *c = (char *)&n;
char low = n & 0x000000ff;
if(c[0] == low)
{
cout << "little endian!" << endl;
}
else
{
cout << "big endian!" << endl;
}
return 0;
}
测试
DEV测试
VS测试
Ubuntu16.04测试