搜索
bottom↓
回复: 7

C语言 如何将IP地址转换为数字

[复制链接]

出0入0汤圆

发表于 2013-8-12 08:01:26 | 显示全部楼层 |阅读模式
如何将IP地址 如192.138.0.10这个IP地址,输入是将其当字符串,转换为192   138  0  10  这四个整数 存储起来

阿莫论坛20周年了!感谢大家的支持与爱护!!

知道什么是神吗?其实神本来也是人,只不过神做了人做不到的事情 所以才成了神。 (头文字D, 杜汶泽)

出0入0汤圆

发表于 2013-8-12 08:05:43 | 显示全部楼层
sscanf                    

出0入0汤圆

发表于 2013-8-12 08:20:26 | 显示全部楼层
查表!!!!!!!!!!!!!!!!!!!

出0入89汤圆

发表于 2013-8-12 08:26:10 | 显示全部楼层
用状态机写一个就好了

出0入0汤圆

发表于 2013-8-12 08:56:38 | 显示全部楼层
转成long(32)或int(32)吧,IP本来就是32位整数来的。

出0入34汤圆

发表于 2013-8-12 10:17:33 | 显示全部楼层
  1. typedef unsigned short ipaddr[2]; /* Conf_IP_Address */

  2. void DisplayIPAddress(unsigned short ipaddr)
  3. {
  4.     char pucBuf[16];
  5.     int iIndex = 0;
  6.     unsigned char *pucTemp = (unsigned char *)ipaddr;
  7.     unsigned char ucValue;

  8.     //
  9.     // Process first byte of IP Address.
  10.     //
  11.     ucValue = pucTemp[0];
  12.     if(ucValue > 9)
  13.     {
  14.         if(ucValue > 99)
  15.         {
  16.             pucBuf[iIndex++] = '0' + (ucValue / 100);
  17.             ucValue %= 100;
  18.         }
  19.         pucBuf[iIndex++] = '0' + (ucValue / 10);
  20.         ucValue %= 10;
  21.     }
  22.     pucBuf[iIndex++] = '0' + (ucValue);
  23.     pucBuf[iIndex++] = '.';

  24.     //
  25.     // Process second byte of IP Address.
  26.     //
  27.     ucValue = pucTemp[1];
  28.     if(ucValue > 9)
  29.     {
  30.         if(ucValue > 99)
  31.         {
  32.             pucBuf[iIndex++] = '0' + (ucValue / 100);
  33.             ucValue %= 100;
  34.         }
  35.         pucBuf[iIndex++] = '0' + (ucValue / 10);
  36.         ucValue %= 10;
  37.     }
  38.     pucBuf[iIndex++] = '0' + (ucValue);
  39.     pucBuf[iIndex++] = '.';

  40.     //
  41.     // Process third byte of IP Address.
  42.     //
  43.     ucValue = pucTemp[2];
  44.     if(ucValue > 9)
  45.     {
  46.         if(ucValue > 99)
  47.         {
  48.             pucBuf[iIndex++] = '0' + (ucValue / 100);
  49.             ucValue %= 100;
  50.         }
  51.         pucBuf[iIndex++] = '0' + (ucValue / 10);
  52.         ucValue %= 10;
  53.     }
  54.     pucBuf[iIndex++] = '0' + (ucValue);
  55.     pucBuf[iIndex++] = '.';

  56.     //
  57.     // Process last byte of IP Address.
  58.     //
  59.     ucValue = pucTemp[3];
  60.     if(ucValue > 9)
  61.     {
  62.         if(ucValue > 99)
  63.         {
  64.             pucBuf[iIndex++] = '0' + (ucValue / 100);
  65.             ucValue %= 100;
  66.         }
  67.         pucBuf[iIndex++] = '0' + (ucValue / 10);
  68.         ucValue %= 10;
  69.     }
  70.     pucBuf[iIndex++] = '0' + (ucValue);
  71.     pucBuf[iIndex] = 0;

  72.     //
  73.     // Display the string.
  74.     //
  75.     Printf(pucBuf);
  76. }
复制代码

出0入0汤圆

 楼主| 发表于 2013-8-12 17:25:37 | 显示全部楼层
qlb1234 发表于 2013-8-12 08:05
sscanf

  谢谢

出0入0汤圆

发表于 2013-8-12 17:40:34 | 显示全部楼层
不明白啊。沒有一個人看懂樓主的問題嗎?
  1. unsigned char a, b, c, d;
  2. char ip[] = "192.168.1.1";
  3. sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d);
复制代码
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-7-23 16:18

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表