java中IP地址轉換十進位數實現代碼

來源:互聯網
上載者:User

先看執行個體

 代碼如下 複製代碼

class ip
{
private static long iptolong(string strip)
//將127.0.0.1 形式的ip地址轉換成10進位整數,這裡沒有進行任何錯誤處理
{
int j=0;
int i=0;
long [] ip=new long[4];
int position1=strip.indexof(".");
int position2=strip.indexof(".",position1+1);
int position3=strip.indexof(".",position2+1);
ip[0]=long.parselong(strip.substring(0,position1));
ip[1]=long.parselong(strip.substring(position1+1,position2));
ip[2]=long.parselong(strip.substring(position2+1,position3));
ip[3]=long.parselong(strip.substring(position3+1));
return (ip[0]<<24)+(ip[1]<<16)+(ip[2]<<8)+ip[3]; //ip1*256*256*256+ip2*256*256+ip3*256+ip4
}
private static string longtoip(long longip)
//將10進位整數形式轉換成127.0.0.1形式的ip地址,在命令提示字元下輸入ping 3396362403l
{
stringbuffer sb=new stringbuffer("");
sb.append(string.valueof(longip>>>24));//直接右移24位
sb.append(".");
sb.append(string.valueof((longip&0x00ffffff)>>>16)); //將高8位置0,然後右移16位
sb.append(".");
sb.append(string.valueof((longip&0x0000ffff)>>>8));
sb.append(".");
sb.append(string.valueof(longip&0x000000ff));
sb.append(".");
return sb.tostring();
}
public static void main(string[] args)
{
system.out.println("ip地址的各種表現形式:rn");
system.out.print("32位二進位形式:");
system.out.println(long.tobinarystring(3396362403l));
system.out.print("十進位形式:");
system.out.println(iptolong("202.112.96.163"));
system.out.print("普通形式:");
system.out.println(longtoip(3396362403l));
}
}

運行結果:

ip地址的各種表現形式:

32位二進位形式:11001010011100000110000010100011
十進位形式:3396362403
普通形式:202.112.96.163.

輸出完成 (耗時 1 秒) - 正常終止

我們再一步步分開來分析一下
知識點:一個位元,按位左移n位,就是把該數的值乘以2的n次方

                 二進位除二即右移一位

1、IP地址轉換為整數

原理:IP地址每段可以看成是8位不帶正負號的整數即0-255,把每段拆分成一個二進位形式組合起來,然後把這個位元轉變成

一個無符號32為整數。

舉例:一個ip地址為10.0.3.193

每段數字 相對應的位元

10     00001010

0       00000000

3       00000011

193   11000001

組合起來即為:00001010 00000000 00000011 11000001,轉換為10進位就是:167773121,即該IP地址轉換後的數字就是它了。

 代碼如下 複製代碼
public class Ip {  
    public static void main(String[] args) {  
        System.out.print(ip2int("10.0.3.193"));  
    }  
 
    public static long ip2int(String ip) {  
       String[] items = ip.split("\.");  
        return Long.valueOf(items[0]) << 24   
                | Long.valueOf(items[1]) << 16 
                | Long.valueOf(items[2]) << 8   
                | Long.valueOf(items[3]);  
    }  
 
} 

2、整數轉換為IP地址

原理:把這個整數轉換成一個32位位元。從左至右,每8位進行一下分割,得到4段8位的位元,把這些位元轉換成整數然後加上”。”就是這個ip地址了

舉例:167773121

二進位表示形式為:00001010 00000000 00000011 11000001

分割成四段:00001010,00001010,00000011,11000001,分別轉換為整數後加上“。”就得到了10.0.3.193。

 代碼如下 複製代碼

public class Ip {  
    public static void main(String[] args) {  
        System.out.print(int2ip(167773121));  
    }  
 
   public static String int2ip(long ipInt) {  
       StringBuilder sb = new StringBuilder();  
        sb.append(ipInt & 0xFF).append(".");  
        sb.append((ipInt >> 8) & 0xFF).append(".");  
        sb.append((ipInt >> 16) & 0xFF).append(".");  
        sb.append((ipInt >> 24) & 0xFF);  
        return sb.toString();  
    }   
 
} 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.