#include<string.h> #include<arpa/inet.h> #include<stdio.h> int main(void){ char ip[]="1.2.3.4";//字符串 struct sockaddr_in server_addr; inet_pton(AF_INET,ip,&server_addr.sin_addr.s_addr);//字符串 to 网络字节序 printf("s_addr : %x\n",server_addr.sin_addr.s_addr); printf("s_addr from to host : %x\n",ntohl(server_addr.sin_addr.s_addr));//网络字节序 to 主机字节序 char server_ip[64]; inet_ntop(AF_INET,&server_addr.sin_addr.s_addr,server_ip,64);//网络字节序 to 字符串 printf("server ip : %s\n",server_ip); return 0; }
结果: