File tree 1 file changed +11
-1
lines changed
src/main/java/com/thealgorithms/conversions 1 file changed +11
-1
lines changed Original file line number Diff line number Diff line change @@ -14,14 +14,24 @@ public final class IPConverter {
14
14
private IPConverter () {
15
15
}
16
16
17
+ /**
18
+ * Converts an IPv4 address to its binary equivalent.
19
+ * @param ip The IPv4 address to convert.
20
+ * @return The binary equivalent of the IPv4 address.
21
+ */
17
22
public static String ipToBinary (String ip ) {
18
23
StringBuilder binary = new StringBuilder ();
19
24
for (String octet : ip .split ("\\ ." )) {
20
- binary .append (String .format ("%08d " , Integer .parseInt ( Integer . toBinaryString (Integer .parseInt (octet ))))).append ("." );
25
+ binary .append (String .format ("%8s " , Integer .toBinaryString (Integer .parseInt (octet ))). replace ( ' ' , '0' )).append ("." );
21
26
}
22
27
return binary .substring (0 , binary .length () - 1 );
23
28
}
24
29
30
+ /**
31
+ * Converts a binary equivalent to an IPv4 address.
32
+ * @param binary The binary equivalent to convert.
33
+ * @return The IPv4 address of the binary equivalent.
34
+ */
25
35
public static String binaryToIP (String binary ) {
26
36
StringBuilder ip = new StringBuilder ();
27
37
for (String octet : binary .split ("\\ ." )) {
You can’t perform that action at this time.
0 commit comments