qfedu-network-advanced-level/day2/n3.c

18 lines
448 B
C
Raw Permalink Normal View History

2023-09-05 20:54:17 +08:00
#include <stdio.h>
#include <arpa/inet.h>
int main(int argc, char const *argv[])
{
unsigned int data = 0x01020304;
// 转成网络端数据(小端转大端)
uint32_t data2 = htonl(data);
printf("主机字节序: %#08x\n", data);
printf("网络字节序: %#x\n", data2);
// 将网络字节序转为主机字节序
uint32_t data3 = ntohl(data2);
printf("转换后主机字节序: %#x\n", data);
return 0;
}