整数IP -> 字符串IP
void ip_to_string
(ulong y
, char *s
)
{
int x
= ntohl
(y
);
sprintf
(s
, "%d.%d.%d.%d",
(int) ((x
>> 24) & 0xff),
(int) ((x
>> 16) & 0xff),
(int) ((x
>> 8) & 0xff), (int) ((x
>> 0) & 0xff)
);
}
字符串IP -> 整数IP
int string_to_ip
(const char* s
)
{
char* token
= NULL;
int nIP
= 0;
char strIP
[16];
memcpy(strIP
, s
, strlen(s
));
strIP
[strlen(s
)] = '\0';
token
= strtok(strIP
, ".");
if(NULL == token
) return 0;
nIP
|= atoi(token
);
for(int i
=1; i
<4; i
++)
{
if(NULL == (token
= strtok(NULL, "."))) return 0;
nIP
|= (atoi(token
) << (8*i
));
}
return nIP
;
}
转载请注明原文地址: https://yun.8miu.com/read-134017.html