要判断大小端,我们首先来了解以下大小端的概念
大端模式:数据的高字节,保存在内存的低地址中。而数据的低字节,保存在高地址中
小段模式:数据的低字节,保存在内存的低地址中,而数据的高字节,保存在高地址中
方法1:利用联合解决:首先要了解联合的存放顺序是所有成员都从低地址开始存放
#include<stdlib.h>
union A
{
int a1;
char b1;
}a;
int main()
{
a.a1 = 1;
if (a.b1 == 1)
{
printf("小端");
}
else
{
printf("大端");
}
system("pause");
return 0;
}
方法二:
#include<stdio.h>
#include<stdlib.h>
int a = 1;
char* p = (char*)&a;
int main()
{
if (*p == 1)
{
printf("小端");
}
else
{
printf("大端");
}
system("pause");
return 0;
}