64,682
社区成员
发帖
与我相关
我的任务
分享
#include <stdio.h>
#include <stdlib.h>
void ntop(char *ip, unsigned int *i_ip)
{
char *p = NULL;
int j = 0;
static int len = 24;
*i_ip = 0;
char temp[3] = {0};
unsigned int buf = 0;
for(p = ip; *p != '\0'; p++)
{
if((*p != '.' )&& (*p != '\0') && (j < 3))
{
temp[j++] = *p;
}
else
{
buf = atoi(temp);
if(j == 1)
buf /= 100;
if(j == 2)
buf /= 10;
buf <<= len;
(*i_ip) |= buf;
len -= 8;
j = 0;
for(int i = 0; i < 3; i++)
temp[i] = '0';
}
}
buf = atoi(temp);
if(j == 1)
buf /= 100;
if(j == 2)
buf /= 10;
(*i_ip) |= buf;
}
int main()
{
char ip[] = "255.255.255.254";
unsigned int i_ip = 0;
ntop(ip, &i_ip);
printf("%u\n", i_ip);
return 0;
}