何为大端小端
大端模式:数据的低位保存在内存的高地址中,而数据的高位保存在内存的低地址中;
小端模式:数据的低位保存在内存的低地址中,而数据的高位保存在内存的高地址中。
如何判断
#include <stdio.h>
int main()
{
union {
int value;
char byte[sizeof(int)];
/* 功能同上行
struct { char c0, c1, c2, c3; } byte;
*/
}test;
test.value = 0x030201;
printf("byte[0]: %p %d\n", &test.byte[0], test.byte[0]);
printf("byte[1]: %p %d\n", &test.byte[1], test.byte[1]);
printf("byte[2]: %p %d\n", &test.byte[2], test.byte[2]);
if(test.byte[sizeof(int)-1] == 1) {
printf("big endian\n");
} else if(test.byte[0] == 1) {
printf("little endian\n");
} else {
printf("unknow...\n");
}
return 0;
}
这段代码涉及到union(联合体)的巧妙使用,虽然采用其它方法也能达到这个目的,但union不需要额外的赋值和类型转换,显得更加优雅和谐高大上。
其它问题
老生常谈的话题,在此就不废话了