代码如下:
#include <stdio.h>
//#include <arpa/inet.h> //linux
#include<WinSock2.h> //windows
int main()
{
unsigned long a = 0x12345678;
unsigned char *p = (unsigned char *)(&a);
printf("主机字节序:%0x %0x %0x %0x\n", p[0], p[1], p[2], p[3]);
unsigned long b = htonl(a); //将主机字节序转化成了网络字节序
p = (unsigned char *)(&b);
printf("网络字节序:%0x %0x %0x %0x\n", p[0], p[1], p[2], p[3]);
return 0;
}
编译报错显示:undefind reference to ‘_imp_htonl’
搜了一下,发现是因为编译时没有加入 -lwsock32 导致的
工具->编译选项->勾选在编译时加入以下命令
-lwsock32