标签:ar for 数据 on ad ef as res har
int inet_aton(const char *cp)
{
int dots = 0;
int a[32]= {0};
register unsigned long acc = 0, addr = 0;
do {
register char cc = *cp;
switch (cc)
{
case ‘0‘:
case ‘1‘:
case ‘2‘:
case ‘3‘:
case ‘4‘:
case ‘5‘:
case ‘6‘:
case ‘7‘:
case ‘8‘:
case ‘9‘:
acc = acc * 10 + (cc - ‘0‘);
break;
case ‘.‘:
if (++dots > 3)
{
return 0;
}
// Fall through
case ‘\0‘:
if (acc > 255)
{
return 0;
}
addr = addr << 8 | acc; // 这句是精华,每次将当前值左移八位加上后面的值 addr 最后结果是个unsigned long 型整数。
acc = 0;
break;
default:
return 0;
}
} while (*cp++) ;
// Normalize the address
if (dots < 3)
{
addr <<= 8 * (3 - dots) ;
}
int k=0;
while (addr)
{
a[k++]=addr%2;
addr/=2;
}
for (int k=31;k>=0; k--)
{
cout <<a[k];
if (k%8==0)
{
cout<<" ";
}
}
return 1;
}
标签:ar for 数据 on ad ef as res har
原文地址:http://www.cnblogs.com/niupan369/p/4045901.html