C语言 如何将IP地址转换为数字
如何将IP地址 如192.138.0.10这个IP地址,输入是将其当字符串,转换为192 138010这四个整数 存储起来 sscanf 查表!!!!!!!!!!!!!!!!!!! 用状态机写一个就好了 转成long(32)或int(32)吧,IP本来就是32位整数来的。 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);
} qlb1234 发表于 2013-8-12 08:05 static/image/common/back.gif
sscanf
{:victory:}谢谢 不明白啊。沒有一個人看懂樓主的問題嗎?unsigned char a, b, c, d;
char ip[] = "192.168.1.1";
sscanf(ip, "%d.%d.%d.%d", &a, &b, &c, &d);
页:
[1]