首页 > 代码库 > Winsock 入门 判读主机字节序 示例

Winsock 入门 判读主机字节序 示例

 

 1 #include <stdio.h>
 2 
 3 union endian_u
 4 {
 5     /*最大成员的长度就是联合成员的长度。联合可以在定义时直接进行初始化,但这个初始化必须是联合第一个成员的类型,所以把unsigned short sval放在第一个成员的原因*/
 6     unsigned short sval;
 7     unsigned char cval[2];
 8 };
 9 
10 int main(int argc, char *argv[])
11 {
12     char *info = "unknow endian";
13     union endian_u t = { 0x1234 };
14 
15     if (t.cval[0] == 0x12 && t.cval[1] == 0x34)
16         info = "big-endian";
17     else if (t.cval[0] == 0x34 && t.cval[1] == 0x12)
18         info = "little-endian";
19 
20     printf("host is %s.\n", info);
21 
22     return 0;
23 }

 

Winsock 入门 判读主机字节序 示例