yangzi8000 发表于 2013-8-12 08:01:26

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

如何将IP地址 如192.138.0.10这个IP地址,输入是将其当字符串,转换为192   138010这四个整数 存储起来

qlb1234 发表于 2013-8-12 08:05:43

sscanf                  

wangqing_gxu 发表于 2013-8-12 08:20:26

查表!!!!!!!!!!!!!!!!!!!

youkebing 发表于 2013-8-12 08:26:10

用状态机写一个就好了

devcang 发表于 2013-8-12 08:56:38

转成long(32)或int(32)吧,IP本来就是32位整数来的。

xyz543 发表于 2013-8-12 10:17:33

typedef unsigned short ipaddr; /* Conf_IP_Address */

void DisplayIPAddress(unsigned short ipaddr)
{
    char pucBuf;
    int iIndex = 0;
    unsigned char *pucTemp = (unsigned char *)ipaddr;
    unsigned char ucValue;

    //
    // Process first byte of IP Address.
    //
    ucValue = pucTemp;
    if(ucValue > 9)
    {
      if(ucValue > 99)
      {
            pucBuf = '0' + (ucValue / 100);
            ucValue %= 100;
      }
      pucBuf = '0' + (ucValue / 10);
      ucValue %= 10;
    }
    pucBuf = '0' + (ucValue);
    pucBuf = '.';

    //
    // Process second byte of IP Address.
    //
    ucValue = pucTemp;
    if(ucValue > 9)
    {
      if(ucValue > 99)
      {
            pucBuf = '0' + (ucValue / 100);
            ucValue %= 100;
      }
      pucBuf = '0' + (ucValue / 10);
      ucValue %= 10;
    }
    pucBuf = '0' + (ucValue);
    pucBuf = '.';

    //
    // Process third byte of IP Address.
    //
    ucValue = pucTemp;
    if(ucValue > 9)
    {
      if(ucValue > 99)
      {
            pucBuf = '0' + (ucValue / 100);
            ucValue %= 100;
      }
      pucBuf = '0' + (ucValue / 10);
      ucValue %= 10;
    }
    pucBuf = '0' + (ucValue);
    pucBuf = '.';

    //
    // Process last byte of IP Address.
    //
    ucValue = pucTemp;
    if(ucValue > 9)
    {
      if(ucValue > 99)
      {
            pucBuf = '0' + (ucValue / 100);
            ucValue %= 100;
      }
      pucBuf = '0' + (ucValue / 10);
      ucValue %= 10;
    }
    pucBuf = '0' + (ucValue);
    pucBuf = 0;

    //
    // Display the string.
    //
    Printf(pucBuf);
}

yangzi8000 发表于 2013-8-12 17:25:37

qlb1234 发表于 2013-8-12 08:05 static/image/common/back.gif
sscanf

{:victory:}谢谢

qlb1234 发表于 2013-8-12 17:40:34

不明白啊。沒有一個人看懂樓主的問題嗎?unsigned char a, b, c, d;
char ip[] = "192.168.1.1";
sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d);
页: [1]
查看完整版本: C语言 如何将IP地址转换为数字