2009-07-18 114 views

答えて

7

アンIPアドレスは安全のために

(a << 24) | (b << 16) | (c << 8) | d 

、としてshift and bit-wise inclusive OR演算子を使用して32ビットの整数値
に変換することができ、a,b,c,dのそれぞれが有効なが...私に知らせてください範囲0-255 - あなたはあなたのコンバージョンを確認することができます。
さらにvalidate the IP address using this regex exampleすることができます。

+0

あなたは<< 24、ない1 << 24 – notnoop

+0

はい、右を意味しました。それはタイプミスでした。 – nik

+0

0と255は無効なIPアドレスコンポーネントです。 –

3

java.net.InetAddressクラスを使用できます。見るべき2つのメソッドは、getByNameとgetAddressです。ここでは、簡単なコード例では、あなたの提案や他のいくつかの情報源を収集

import java.net.InetAddress; 
import java.net.UnknownHostException; 
/* ... */ 
String ip = "192.168.1.1"; 
InetAddress address = null; 
try { 
    address = InetAddress.getByName(ip); 
} catch (UnknownHostException e) { 
    //Your String wasn't a valid IP Address or host name 
} 
byte [] binaryIP = address.getAddress(); 
1

ですが、私は便利に計算し、()するのを助けることができるビットの配列だけでなく、たBitSetにInetAdressを変換することが判明、または( )、バイナリ表現のxor()。

次のサンプルは、ipをバイナリおよびバイナリにipに変換する方法を示しています。

お楽しみください!

public class IpConverter { 
public static void main(String[] args) { 
    String source = "192.168.1.1"; 
    InetAddress ip = null; 
    try { 
     ip = InetAddress.getByName(source); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
     return; 
    } 
    System.out.println("source : " + ip); 

    // To bit sequence ------------ 
    byte[] binaryIP = ip.getAddress(); 
    BitSet[] bitsets = new BitSet[binaryIP.length]; 
    int k = 0; 

    System.out.print("to binary: "); 
    for (byte b : binaryIP) { 
     bitsets[k] = byteToBitSet(b); 
     System.out.print(toString(bitsets[k]) + "."); 
     k++; 
    } 
    System.out.println(); 

    // Back to InetAdress --------- 
    byte[] binaryIP2 = new byte[4]; 
    k = 0; 
    for (BitSet b : bitsets) { 
     binaryIP2[k] = bitSetToByte(b); 
     k++; 
    } 

    InetAddress ip2 = null; 
    try { 
     ip2 = InetAddress.getByAddress(binaryIP2); 
    } catch (UnknownHostException e) { 
     e.printStackTrace(); 
     return; 
    } 

    System.out.println("flipped back to : " + ip2); 
} 

public static BitSet byteToBitSet(byte b) { 
    BitSet bits = new BitSet(8); 
    for (int i = 0; i < 8; i++) { 
     bits.set(i, ((b & (1 << i)) != 0)); 
    } 
    return bits; 
} 

public static byte bitSetToByte(BitSet bits) { 
    int value = 0; 
    for (int i = 0; i < 8; i++) { 
     if (bits.get(i) == true) { 
      value = value | (1 << i); 
     } 
    } 
    return (byte) value; 
} 

public static byte bitsToByte(boolean[] bits) { 
    int value = 0; 
    for (int i = 0; i < 8; i++) { 
     if (bits[i] == true) { 
      value = value | (1 << i); 
     } 
    } 
    return (byte) value; 
} 

public static boolean[] byteToBits(byte b) { 
    boolean[] bits = new boolean[8]; 
    for (int i = 0; i < bits.length; i++) { 
     bits[i] = ((b & (1 << i)) != 0); 
    } 
    return bits; 
} 

public static String toString(BitSet bits){ 
    String out = ""; 
    for (int i = 0; i < 8; i++) { 
     out += bits.get(i)?"1":"0";   
    } 
    return out; 
} 

}

関連する問題