Convert IP to Binary in Java
Fundamental conversion, such as going from binary to decimal or vice versa, is a crucial activity in computers. Understanding IP addressing and subnetting is crucial for networking. The primary networking function is IP addressing. Finding the network or host ID using a subnet is a crucial operation for a network engineer when issuing IP numbers.
Simple activities are easier to complete when you have a firm grasp of the fundamentals. We will go over how to translate an IP address into binary in this part.
Converting IP Address into Binary
Here are the methods for memorizing-free binary conversion of a decimal IP address.
Start with the number 1 and count from right to left to help you remember values. Multiply that amount seven times.
- Start, for instance, with 1 on the right side. The second number will be twice as large as the first, or 1*2=2. Double the previous number (2) to get the third number, which equals 4 (2*2). For the fourth number, multiply the previous number (4) by two to get eight. Continue until the original number has been multiplied by seven times.
- Arrange the values row-wise after seven times of doubling the number. because we'll give the values either a 0 or a 1.
- We will take the string of integers and work our way clockwise around them in order to translate the IP address into binary. We ask, "Can I remove this number from the decimal remaining?" for each value. Put 1 there if you answered "yes," otherwise, put 0.
- Another option to enter 0 or 1 is to add them together to obtain the IP address' decimal value. Put 1 below those values and 0 below the other values if we arrived at the decimal value by adding three values.
Example
As an illustration, we must translate the following IP address into binary.
192.168.32.2
Each symbol denotes an 8-bit value, or a 32-bit IP address.
8-bit data should be represented by 2x, where x stands for x+1, x+2, x+3, etc. Write the values in 8-bit format.
2^0 = 1
2^1 = 2
2^2 = 4
2^3 = 8
2^4 = 16
2^5 = 32
2^6 = 64
2^7 = 128
1 2 4 8 16 32 64 128
After adding 64 and 128 to reach 192, we shall write 1-1 under 64 and 128 and 0 under the remaining integers. As a result, the binary representation of 192 is:
192: 00000011
The numbers 8, 32, and 128 sum up to 168, thus we'll put 1-1 after 8, 32, and 128. As a result, the binary representation of 168 is:
168: 00010101
Likewise, the binary representation of 32 is:
32: 00000100
Likewise, the binary representation of 2 is:
2: 01000000
Keep in mind that each segment's binary should be written backwards when the IP is combined in binary. As a result, IP 192.168.32.2 is represented in binary as 11000000.10101000.00100000. 00000010.
//
import java.util.Scanner;
public class ITBC
{
public static void main (String args[])
{
Scanner sr = new Scanner(System.in);
System.out.println("Please enter an IP Address.");
//read s address from the user
String st = sr.nextLine();
//split s address by dot(.)
String[] octetArray = st.split("\\.");
for (String string : otArray)
{
//determines a string's primitive data type.
int ot = Integer.parseInt(st);
//translates a binary st from an integer
String bO = Integer.toBinaryString(octet);
//displays the appropriate IP address binary string.
System.out.println(bO);
}
}
}
Output:
Please enter an IP Address.
192.168.32.2
11000000
10101000
100000
10
Using IP Address java library
It is an open-source Java library that manages subnets and IP addresses. Both IPv4 and IPv6 addresses are compatible.
<dependency>
<groupId>com.github.seancfoley</groupId>
<artifactId>ipaddress</artifactId>
<version>5.3.4</version>
</dependency>
//
import java.util.Scanner;
import java.util.NoSuchElementException;
import inet.ipaddr.AddressStringException;
import inet.ipaddr.IPAddress;
import inet.ipaddr.IPAddressString;
public class CITB
{
public static void main(String args[])
{
try(Scanner sr = new Scanner(System.in))
{
System.out.print("Enter IP address: ");
while(true)
{
String sc = scan.next();
IPAS s = new IPAS(s);
IPA address = s.toAddress();
System.out.println(address + " is equivalent to " + address.toBS());
}
}
catch (ASException e)
{
System.out.println("invalid format: " + e.getMsg());
}
catch(NoSuchElementException e)
{
}
}
}
Output 1:
Enter IP: 235.145.38.12
235.145.38.12 is equivalent to 11101011100100010010011000001100
Output 2:
Enter IP: a:b:c:d:e:f:a:b
a:b:c:d:e:f:a:b is equivalent to 00000000000010100000000000001011000000000000110