inet_pton、inet_ntop的用法
作者:互联网
函数定义
const char * inet_ntop(int af, const void * restrict src, char * restrict dst, socklen_t size);
int inet_pton(int af, const char * restrict src, void * restrict dst);
示例
#include <stdio.h>
#include <arpa/inet.h>
int main()
{
char *ipv6 = "2409:8a1e:6a62:e440:4f:bbe7:a27e:28e8";
struct in6_addr ip6;
char test[64] = {0};
inet_pton(AF_INET6, ipv6, &ip6);
printf("%x-%x-%x-%x\n", ip6.__u6_addr.__u6_addr32[0], ip6.__u6_addr.__u6_addr32[1], ip6.__u6_addr.__u6_addr32[2], ip6.__u6_addr.__u6_addr32[3]);
// ip6.s_addr = 0x00001e8a;
inet_ntop(AF_INET6, &ip6, test, 64);
printf("test:%s\n", test);
char *ipv4 = "192.168.1.1";
struct in_addr ip4;
inet_pton(AF_INET, ipv4, &ip4);
printf("%x\n", ip4.s_addr);
return 0;
}
标签:.__,addr,u6,ntop,char,pton,ip6,inet 来源: https://www.cnblogs.com/st17/p/13967353.html