标签:
#include<stdio.h>
#include<arpa/inet.h>
#include<stdlib.h>
void pton(const char* src)
{
struct in_addr addr;
int temp = inet_pton(AF_INET,src,&addr);
if(temp<=0)
{
perror("fail to convert");
exit(-1);
}
printf("convert success,ip:%x\n",addr.s_addr);
}
void ntop(struct in_addr addr)
{
char dst[16]={0}; // 255.255.255.255 3*4+3(‘.‘)+1(‘\0‘)=16
const char* temp = inet_ntop(AF_INET,&addr,&dst,sizeof(dst));
if(temp==NULL)
{
perror("fail to convert");
exit(-1);
}
printf("convert success,ip:%s\n",dst);
}
int main()
{
pton("192.168.1.24");
struct in_addr addr;
addr.s_addr = 0x1801a8c0;
ntop(addr);
return 0;
}
标签:
原文地址:http://www.cnblogs.com/ZhangJinkun/p/4570484.html