Skip to content

Commit a5902cc

Browse files
committed
Fix
1 parent 5e44f2d commit a5902cc

File tree

1 file changed

+11
-1
lines changed

1 file changed

+11
-1
lines changed

src/main/java/com/thealgorithms/conversions/IPConverter.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,24 @@ public final class IPConverter {
1414
private IPConverter() {
1515
}
1616

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+
*/
1722
public static String ipToBinary(String ip) {
1823
StringBuilder binary = new StringBuilder();
1924
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(".");
2126
}
2227
return binary.substring(0, binary.length() - 1);
2328
}
2429

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+
*/
2535
public static String binaryToIP(String binary) {
2636
StringBuilder ip = new StringBuilder();
2737
for (String octet : binary.split("\\.")) {

0 commit comments

Comments
 (0)