linux cサイズエンドバイトシーケンスについて

952 ワード

サイズエンドバイト変換
#include 
 
union test   //           (          )
{           
	int a;
	char b;
};
 
int main()
{
	printf("%d
", sizeof(union test));//union union , 。 union test t; t.a = 1; t.a = ((t.a & 0x000000ff) << 24) | ((t.a & 0x0000ff00) << 8) |((t.a & 0x00ff0000) >> 8) |((t.a & 0xff000000) >> 24);// ( ) printf("%d
", t.a); return 0; }

判定サイズ端
#include 
 
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!
"); } else if (t.array[0] == 2 && t.array[1] == 1)// : , { printf("little endian!
"); } return 0; }