1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <malloc.h>
  5. int main(int argc,char *argv[])
  6. {
  7. const char *ip ="192.168.34.232";
  8. char *ipstr=NULL;
  9. char str_ip_index[4]={'\0'};
  10. unsigned int ip_int,ip_add=0,ip_int_index[4],ip_temp_numbr=24;
  11. int j =0,a=3;
  12. for(unsigned int i =0;i<=strlen(ip);i++)//要用到'\0'
  13. {
  14. if (ip[i]=='\0'||ip[i]=='.')
  15. {
  16. ip_int =atoi(str_ip_index);
  17. if (ip_int<0||ip_int>255)
  18. {
  19. printf("IP地址有误\n");
  20. system("pause");
  21. return 0;
  22. }
  23. ip_add+=(ip_int*((unsigned int)pow(256.0,a)));
  24. a--;
  25. memset(str_ip_index,0,sizeof(str_ip_index));
  26. //          for (int x=0;x<4;x++)
  27. //          {
  28. //              str_ip_index[x]='\0';
  29. //          }
  30. j=0;
  31. continue;
  32. }
  33. str_ip_index[j]=ip[i];
  34. j++;
  35. }
  36. printf("%u\n",ip_add);
  37. //转换成x.x.x.x
  38. for(j=0;j<4;j++)
  39. {
  40. ip_int_index[j]=(ip_add>>ip_temp_numbr)&0xFF;
  41. ip_temp_numbr-=8;
  42. }
  43. if ((ipstr=(char *)malloc(17*sizeof(char)))==NULL)
  44. {
  45. return 0;
  46. }
  47. sprintf(ipstr,"%d.%d.%d.%d",ip_int_index[0],ip_int_index[1],ip_int_index[2],ip_int_index[3]);
  48. printf("%s\n",ipstr);
  49. free(ipstr);
  50. ipstr=NULL;
  51. system("pause");
  52. return 0;
  53. }

转换效果:

C语言实现字符串IP与整数型IP的相互转换的更多相关文章

  1. python实现判断一个字符串是否是合法IP地址

    #!usr/bin/env python #encoding:utf-8 ''''' __Author__:沂水寒城 功能:判断一个字符串是否是合法IP地址 ''' import re def jud ...

  2. 判断一个字符串是否是合法IP地址

    # -*- coding: utf-8 -*- """ @File:test06_判断ip地址是否合法.py @E-mail:364942727@qq.com @Time ...

  3. Java使用纯真IP库获取IP对应省份和城市

    原文:http://blog.csdn.net/chwshuang/article/details/78027873?locationNum=10&fps=1 Java使用纯真IP库获取IP对 ...

  4. go语言之字符串、指针、数组、切片、结构struct、面向对象

    一: 字符串 概述: Go 语言将字符串作为 种原生的基本数据类型,字 符串的初始化可以使用字符串字面量. (1)字符串是常量,可以通过类 数组 索引访问其字节单元,但是不能修改某个字节的值 (2)宇 ...

  5. [LeetCode] Validate IP Address 验证IP地址

    In this problem, your job to write a function to check whether a input string is a valid IPv4 addres ...

  6. [LeetCode] Restore IP Addresses 复原IP地址

    Given a string containing only digits, restore it by returning all possible valid IP address combina ...

  7. c#中获取服务器IP,客户端IP以及Request.ServerVariables详细说明

    客户端ip: Request.ServerVariables.Get("Remote_Addr").ToString();  客户端主机名: Request.ServerVaria ...

  8. C#获取本机IP以及无线网ip

       1 private void GetIP()   2 { 3 string hostName = Dns.GetHostName();//本机名 4 //System.Net.IPAddress ...

  9. node.js使用免费的阿里云ip查询获取ip所在地

    在项目过程中,我们常常需要获取IP的所在地.而这一功能一般都是通过一些数据网站的对外接口来实现,这些接口一般情况下都是付费使用的.在这篇文章中我将记录,基于node.js的阿里云免费IP地址查询接口的 ...

随机推荐

  1. FileOutPutStream in 创新实训 自然语言交流系统

    FileOutPutStream在c盘等一级目录下是可以创建文件的,如: new FileOutputStream("c:\\kk.txt");但是在c\\test等就创建不了,F ...

  2. powerDesigner生成Html及Word

    转载地址:https://blog.csdn.net/zdp072/article/details/51900794 1:点击Reports 2:点击New Reports 3:定义名字,选择简体中文 ...

  3. org.in2bits.MyXls.XlsDocument 生成excel文件 ; 如果想读取模板再另外生成的话,试试 NPOI

    优点:不依赖Microsoft组件,在内存中操作excel,读写速度快.   缺点:无法读取模板,只能生成新的excel (我亲自测试是在读取完模板后,不能保存,也不能另存,并且其他人说这个读取还会有 ...

  4. 查看CentOS系统运行了多久使用uptime命令

    对于一些人来说系统运行了多久是无关紧要的,但是对于服务器管理员来说,这是相当重要的信息. 服务器在运行重要应用的时候,必须尽量保证长时间的稳定运行,有时候甚至要求零宕机. 那么我们怎么才能知道服务器运 ...

  5. SqlParameter 之 in

    List<string> ConditionList = new List<string>(); List<string> tempParameters = new ...

  6. ZeroClipboard插件——复制到剪切板

    ZeroClipboard是一个轻量级的jQuery“复制到剪贴板”插件采用了时下流行的零剪贴板库.官网:http://www.steamdev.com/zclip 参数及默认值path(必选)  Z ...

  7. js pjax 和window.history.pushState,replaceState

    原文:http://blog.linjunhalida.com/blog/pjax/ github:https://github.com/defunkt/jquery-pjax 什么是pjax? 现在 ...

  8. 在PHP中使用curl_init函数的说明

    $ch = curl_init(); $c_url = 'http://www.baidu.com'; $c_url_data = "product_&type=".$ty ...

  9. 内存管理 初始化(三)before mm_init()

    看到了mm_init(),期间将从bootmem迁移到伙伴系统,slab分配器也会建立. 在分析mm_init()之前,把setup_arch(&command_line)之后的函数分析了以下 ...

  10. 【NLP】simhash判断文档相似度

    http://blog.csdn.net/heiyeshuwu/article/details/44117473