点分十进制IP和长整型转换

【Programming Clip】点分十进制IP和长整型转换

 作者:gnuhpc 
出处:http://www.cnblogs.com/gnuhpc/ 
 

1.用途

将一个整型数值和一个IP字符串相互转换。

2.描述语言

C, Java

3.原理

IP地址是一个以点作为分隔符的十进制四字段字符串,例如“10.0.3.193”。将这四个十进制数转化为二进制即为:

每段数字             相对应的二进制数 
10                          00001010 
0                            00000000 
3                            00000011 
193                       11000001

以从左到右的顺序放在一起,为00001010 00000000 00000011 11000001,转换为10进制数就是:167773121,即为一个长整型。

从长整型到字符串的转化要点:移位、屏蔽掉不需要的位,字符串拼接。在C语言中可以使用指针巧妙的封装移位操作。

从字符串到长整型的转化要点:解析字符串,移位,求和。

4.代码

C语言描述:

  1. /* 
  2.  * ===================================================================================== 
  3.  * 
  4.  *       Filename:  ipconverter.cpp 
  5.  * 
  6.  *    Description:   
  7.  * 
  8.  *        Version:  1.0 
  9.  *        Created:  01/08/2012 11:02:12 PM 
  10.  *       Revision:  none 
  11.  *       Compiler:  gcc 
  12.  * 
  13.  *         Author:  gnuhpc (http://www.cnblogs.com/gnuhpc), warmbupt@gmail.com 
  14.  *        Company:  CMBC 
  15.  * 
  16.  * ===================================================================================== 
  17.  */  
  18. #include <iostream>  
  19. #include <cstdio>  
  20. #include <cstdlib>  
  21. #include <cstring>  
  22.   
  23. using namespace std;  
  24.   
  25.   
  26. char* itoa(int i)  
  27. {  
  28.     char* temp = new char(6);  
  29.     sprintf(temp,"%d",i);  
  30.     return temp;  
  31. }  
  32.   
  33. char* numToIP1(unsigned int num)  
  34. {  
  35.     char* buf = new char[sizeof("aaa.bbb.ccc.ddd")];  
  36.     unsigned char* p = (unsigned char *)&num;  
  37.     sprintf(buf, "%d.%d.%d.%d", p[3]&0xff,p[2]&0xff,p[1]&0xff,p[0]&0xff);  
  38.     return buf;  
  39. }  
  40. char* numToIP2(unsigned int num)  
  41. {  
  42.     char* buf = new char[sizeof("aaa.bbb.ccc.ddd")];  
  43.     for(int i=3;i>=0;i--)  
  44.     {  
  45.         strcat(buf, itoa((num>>8*i)&0xff));  
  46.         if(i!=0)  
  47.             strcat(buf,".");  
  48.     }  
  49.   
  50.     return buf;  
  51. }  
  52.   
  53. unsigned int ipToNum(char* ip)  
  54. {  
  55.     char* p;  
  56.     int sections[4]={0};  
  57.     int i=0;  
  58.   
  59.     p = strtok(ip,".");  
  60.     while( p )  
  61.     {  
  62.         sections[i] = atoi(p);  
  63.         p = strtok(NULL,".");  
  64.         cout << sections[i] << endl;  
  65.         i++;  
  66.     }  
  67.   
  68.     unsigned int num =0;  
  69.     forint j=3,i=0 ; j>=0 ; j--,i++ )  
  70.     {  
  71.         num += (sections[i] <<(8*j));  
  72.     }  
  73.       
  74.     return num;  
  75. }  
  76.   
  77. int main(){  
  78.     char* p = numToIP1(167773121);  
  79.     cout << p << endl;  
  80.     delete p;  
  81.   
  82.     p = numToIP2(167773121);  
  83.     cout << p << endl;  
  84.     delete p;  
  85.   
  86.   
  87.     char ip[16] = "10.0.3.193";  
  88.     cout << ipToNum(ip) << endl;  
  89.     return 0;  
  90. }  

Java描述:

  1. package cnblogs.gnuhpc.ipconvertor;  
  2.   
  3. public class IPConvertor {  
  4.     public static String numToIP(long ip){  
  5.         StringBuilder sb = new StringBuilder();  
  6.         for (int i = 3; i >=0; i--) {  
  7.             sb.append((ip>>>(i*8))&0x000000ff);  
  8.             if (i!=0) {  
  9.                 sb.append('.');  
  10.             }  
  11.         }  
  12.         System.out.println(sb);  
  13.         return sb.toString();  
  14.     }  
  15.       
  16.     public static long ipToNum(String ip){  
  17.         long num = 0;  
  18.         String[] sections = ip.split("\\.");  
  19.         int i=3;  
  20.         for (String str : sections) {  
  21.             num+=(Long.parseLong(str)<<(i*8));  
  22.             i--;  
  23.         }  
  24.         System.out.println(num);  
  25.         return num;  
  26.     }  
  27. }  

5.收获

1)C语言中unsigned int类型为4字节,范围为0 -> +4,294,967,295 ,而long int也为4字节,只是其从0开始,范围为-2,147,483,648 -> +2,147,483,647 。Java中没有unsigned类型,long类型为8 字节,范围为 -9,223,372,036,854,775,808 to +9,223,372,036,854,775,807.

2)Java中String.split方法,如果用“.”作为分隔的话,必须采用:String.split("\\."),这样才能正确的分隔开,不能用String.split(".")。

3)Java中的位移:

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值