views:

1446

answers:

3

Does anyone knows how to convert decimal notation of an IP address into binary form in Java? Please let me know...

+4  A: 

An IP address written as a.b.c.d can be converted to a 32-bit integer value
using shift and bit-wise inclusive OR operators as,

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

To be safe, each of a,b,c,d has valid range 0-255 -- you can check that in your conversion.
You can further validate the IP address using this regex example.

nik
You meant a<<24, not 1<<24
notnoop
Yes, right. It was a typo.
nik
0 and 255 are NOT valid IP address components.
Vladimir Dyuzhev
@Vladimir, Not always, but will you call `192.168.0.1` valid or not? That is why I gave the Validate IP Address reference. I understand you mean `a.b.c.0` and `a.b.c.255` are not exactly valid addresses.
nik
Rrrright! My bad. And correct, 255 is not IP, it's a mask.
Vladimir Dyuzhev
http://en.wikipedia.org/wiki/IPv4#Addresses_ending_in_0_or_255
iny
Isn't 192.168.0.255 a valid address if your using 192.168.0.0/22 ?
Brad Gilbert
+3  A: 

You can use the java.net.InetAddress class. Two methods you should look at are getByName and getAddress. Here is a simple code example

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();
Rob
A: 

Hi all,

Gathering your suggestions and some other sources, I found usefull to convert an InetAdress to an array of bit, as well as BitSet, which can help to compute and(), or(), xor() out of your binary representation.

Following sample shows how to convert ip to binary and binary to ip.

Enjoy!

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;
}

}

Martin