首页 > 编程语言 > 详细

linux c/c++ IP字符串转换成可比较大小的数字

时间:2014-11-02 22:12:08      阅读:389      评论:0      收藏:0      [点我收藏+]

由www.169it.com搜集整理 

IP字符串转换成可比较大小的数字,具体代码如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdio.h"
#include "arpa/inet.h"
#include
using namespace std;
// linux c/c++ IP字符串转换成可比较大小的数字
// g++ -o test_ip_unsigned test_ip_unsigned.cpp
int main(int argc, char *argv[])
{      
    string ip="192.168.1.123";
    unsigned int x=ntohl(inet_addr(ip.c_str()));
    unsigned int y=(unsigned int)192*256*256*256+168*256*256+256+123; 
    printf("x=[%u] ip un[%u]\r\n",x,y);
    return 1;
}

 

 文章来源:linux c/c++ IP字符串转换成可比较大小的数字

linux c/c++ IP字符串转换成可比较大小的数字

原文:http://www.cnblogs.com/besty/p/4069904.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!