Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1613806
  • 博文数量: 245
  • 博客积分: 10378
  • 博客等级: 上将
  • 技术积分: 2571
  • 用 户 组: 普通用户
  • 注册时间: 2009-03-27 08:19
文章分类

全部博文(245)

文章存档

2013年(4)

2012年(8)

2011年(13)

2010年(68)

2009年(152)

分类: C/C++

2009-03-27 08:45:36

#include
#include

int main(void)
{
 char ip_str[28];
 char *p;
 int ip_num[4]={0},i=0;
 printf("plesa input ip : \n");
 scanf("%s",ip_str);
 printf("current str is %s\n,convertion is starting now...\n",ip_str);
    p=strtok(ip_str,".");
 ip_num[0]=atoi(p);
 for(i=1;i<4;i++)
 {
  p=strtok(NULL,".");
  ip_num[i]=atoi(p);
 }
 
 printf("convertion have been done, \n ip address is :\n");
 for(i=0;i<4;i++)
 {
  printf("%d",ip_num[i]);
  if(i!=3)
  printf(".");
 }
 puts("\n");
 
 
 return 0;
}


 

 

以上的操作过于复杂,不推荐,现在又更简单的方法:

sscanf(ipaddr,"%d.%d.%d.%d",ip1,ip2,ip3,ip4);

spirntf(ip,"%d.%d.%d.%d",ip1,ip2,ip3,ip4);

就这样就可以将ipaddr中存储的字符串ip放入到ipaddr中,由字符串到数字格式转换就这么简单。

阅读(1602) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~