Javatpoint Logo
Javatpoint Logo

Convert IP to Binary in Java

In computers, fundamental conversion (like decimal to binary or vice-versa) is an important task. In networking, it is important to understand IP addressing and subnetting. IP addressing is the main functionality of networking. For a network engineer, assigning IP addresses, determining the network or host ID via a subnet is a vital task.

Having a solid understanding of simple things makes more complex tasks easier. In this section, we will discuss how to convert an IP address into binary.

Convert IP to Binary in Java

Converting IP Address into Binary

Here are steps on how to convert a decimal IP address to its binary form, without memorization.

In order to remember values, start with the number 1, and move from right to left. Double that number seven times.

For example, start with 1 on the right side. The next number (second) will be double the first one i.e. 1*2=2. For the third number, double the last number (2) i.e. 2*2 = 4. For the fourth number, double the last number (4) i.e. 4*2 = 8. Repeat the process until we have doubled the original number seven times.

After doubling the number seven times, arrange the values row-wise. Because we are going to assign either 0 or 1 to the values.

In order to convert the IP address into binary, we will take the string of numbers and start from left to right. For each value, we have a question, "can I subtract this value from the decimal remaining?" If Yes, put 1 there, else put 0.

Another way to put 0 or 1 is that adding which values we get the decimal value of the IP address. If we get the decimal value by adding three values, put 1 below them and put 0 below the rest of the values.

Let's understand the above steps through an example.


For example, we have to convert the following IP address into binary.

Each represents 8-bit means a 32-bit IP address.

Represent 8-bit by 2n where n denotes the power n, n+1, n+2, n+3, …, and so on. Let's write the values in 8-bit.

20 = 1

21 = 2

22 = 4

23 = 8

24 = 16

25 = 32

26 = 64

27 = 128

For convenience write the value row-wise:

1          2          4          8          16        32        64        128

We get 192 by adding 64 and 128, so we will write 1-1 below the 64 and 128 and below the rest values, we will write 0. Therefore, for 192 the binary representation is:

192: 00000011

We get 168 by adding the values 8, 32, and 128, so we will write 1-1 below 8, 32, and 128. Therefore, for 168 the binary representation is:

168: 00010101

Similarly, for 32 the binary representation is:

32: 00000100

Similarly, for 2 the binary representation is:

2: 01000000

Note that when we combine the IP in binary, write the binary in reverse of each segment. Hence, the binary representation of IP is 11000000.10101000.00100000. 00000010.

Java Program to Convert IP to Binary


Please enter an IP Address.

Note: The above program only converts the IPv4 addresses into binary.

Using IPAddress Java Library

It is an open-source Java library used for handling IP addresses and subnets. It is compatible with both IP addresses IPv4 and IPv6.

Note: Before running the following program ensure that the ipaddress-5.3.4.jar file is attached into your project. We can also add dependency into pom.xml file.

Output 1:

Enter IP address: is equivalent to 11101011100100010010011000001100

Output 2:

Enter IP address: a:b:c:d:e:f:a:b
a:b:c:d:e:f:a:b is equivalent to 00000000000010100000000000001011000000000000110000000000000011010000000000001110000000000000111100000000000010100000000000001011

Next TopicJava Macro

Youtube For Videos Join Our Youtube Channel: Join Now


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Trending Technologies

B.Tech / MCA