#include
#include
char *long2ip(u_long in);
u_long ip2long(char *ip);
int main()
{
char p[16];
printf("請(qǐng)輸入IP地址:\n");
scanf("%s",p);
u_long l = ip2long(p);
printf("%s\n",long2ip(l));
int inum = 0 ;
scanf("%d",inum);
return 0;
}
char *long2ip(u_long in)
{
u_char *p;
p = (u_char*)(&in);
static char output[16];
_snprintf_s(output,16,16,"%d.%d.%d.%d",p[0],p[1],p[2],p[3]);
return output;
}
u_long ip2long(char *ip)
{
u_char addr[16];
sscanf(ip,"%d.%d.%d.%d",addr,addr+1,addr+2,addr+3);
u_long *vl = (u_long*)(&addr);
return *vl;
}
相關(guān)推薦:
二級(jí)C語言:typedefPACKEDstruct字節(jié)對(duì)齊詳解 計(jì)算機(jī)等考二級(jí):關(guān)于C語言中無符號(hào)數(shù)和有符號(hào)數(shù) C語言中標(biāo)準(zhǔn)輸入流、標(biāo)準(zhǔn)輸出流、標(biāo)準(zhǔn)錯(cuò)誤輸出流