How do I convert the decimal representation of an IP address to binary?

Does anyone know how to convert IP address decimal notation to binary in Java? Please let me know...

+2


source to share


3 answers


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 , like,

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

      



To be on the safe side, each a,b,c,d

has a valid range 0-255

- you can check this in your transform.
You can check the IP address with this regex example .

+7


source


You can use the java.net.InetAddress class . The two methods you should look out for are getByName and getAddress. Here is a simple example code



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();

      

+3


source


Collecting your suggestions and some other sources, I found it helpful to convert the InetAdress to an array of bits, as well as a BitSet that can help compute and (), or (), xor () from your binary representation.

The following example 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;
}

      

}

+1


source







All Articles