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

23 lines
566 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[])
{
char *ip = "10.35.188.66";
// ip 地址转换为四字节
uint32_t ip_data;
// 将 ip 转换为数值
int ret = inet_pton(AF_INET, ip, &ip_data);
printf("ip转换数值的结果(%d): %#x\n", ret, ip_data);
// 将 ip 的数值转换为 ipv6 字符串
char ip_dst[25] = "";
if (inet_ntop(AF_INET6, &ip_data, ip_dst, INET6_ADDRSTRLEN) != NULL)
{
printf("由数值转化为 ip 地址的字符串: %s\n", ip_dst);
}
return 0;
}