字节序
字节序是指多字节数据在计算机内存中存储或者网络传输时各字节的存储顺序。
大端字节序( Big endian)
大端存储模式是指数据的高字节保存在内存的低地址中,而数据的低字节保存在内存的高地址中。
小端字节序(Little endian)
小端存储模式是指数据的高字节保存在内存的高地址中,而数据的低字节保存在内存的低地址中。
代码如下:
#include<stdio.h>
代码1
int IsLittle()
{
int a = 0x11223344;
char *pc = (char *)&a;
if (*pc == 0x44)
{
return 0;
}
return 1;
}
代码2
union Un
{
int a;
char ch;
};
int IsLittle()
{
union Un uu;
uu.a = 0x11223344;
if (uu.ch == 0x44)
{
return 0;
}
return 1;
}
int main()
{
int ret = IsLittle();
if (ret == 0
{
printf("is little\n");
}
else
{
printf("is big\n");
}
return 0;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)