您的位置:首页 > 编程语言 > C语言/C++

ip与int之间的转化函数

2016-07-18 17:39 495 查看
/*
* purpose : transfer ip to u_int32_t
* @Param IPdotdec : ip
* return u_int_32 : the result u_int32_t
*/
u_int32_t ip2int(char IPdotdec[20]){
struct in_addr s; // IP to int
inet_pton(AF_INET, IPdotdec, (void *)&s);
return s.s_addr;
}

/*
* purpose : transfer u_int32_t to ip
* @Param u_int32_t : ip
* return char * : IPdotdec
*/
void int2ip(u_int32_t ip , char IPdotdec[20]){
struct in_addr s; // int to IP
s.s_addr = ip;
// int2ip
inet_ntop(AF_INET, (void *)&s, IPdotdec, 16);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  C++