Convert IP to Binary in JavaIn computers, fundamental conversion (like decimal to binary or viceversa) 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. Converting IP Address into BinaryHere 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 rowwise. 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. ExampleFor example, we have to convert the following IP address into binary. 192.168.32.2 Each represents 8bit means a 32bit IP address. Represent 8bit by 2^{n} where n denotes the power n, n+1, n+2, n+3, …, and so on. Let's write the values in 8bit. 2^{0 }= 1 2^{1 }= 2 2^{2 }= 4 2^{3 }= 8 2^{4 }= 16 2^{5 }= 32 2^{6 }= 64 2^{7 }= 128 For convenience write the value rowwise: 1 2 4 8 16 32 64 128 We get 192 by adding 64 and 128, so we will write 11 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 11 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 192.168.32.2 is 11000000.10101000.00100000. 00000010. Java Program to Convert IP to BinaryIpToBinaryConverter.java Output: Please enter an IP Address. 192.168.32.2 11000000 10101000 100000 10 Note: The above program only converts the IPv4 addresses into binary.Using IPAddress Java LibraryIt is an opensource 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 ipaddress5.3.4.jar file is attached into your project. We can also add dependency into pom.xml file.ConvertIpToBinary.java Output 1: Enter IP address: 235.145.38.12 235.145.38.12 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
