判断大小端字节序的方法代码

字节序

1、大端字节序:高字节存放在低地址,低字节存放在高地址。

2、小端字节序:高字节存放在高地址,高字节存放在低地址。

★:字节序由计算机决定,可以用代码查询。

​
#include <stdio.h>

union test
{
	short val;
	char array[2];
};

int main()
{
	union test t;

	t.val = 0x0102;

	if (t.array[0] == 1 && t.array[1] == 2)
	{
		printf("big endian!\n");  //大端
	}
	else if (t.array[0] == 2 && t.array[1] == 1)
	{
		printf("little endian!\n"); //小端
	}

	return 0;
}

​

猜你喜欢

转载自blog.csdn.net/wow66lfy/article/details/81276714