Skip to content

Commit 3e02108

Browse files
author
alxkm
committed
refactor: fix representation according to general standard
1 parent 0fde3bf commit 3e02108

File tree

2 files changed

+15
-2
lines changed

2 files changed

+15
-2
lines changed

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

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,19 @@ public static String decToHex(int decimal) {
2424
hexBuilder.insert(0, HEX_DIGITS[currentHalfByte]);
2525
decimal >>= NUMBER_OF_BITS_IN_HALF_BYTE;
2626
}
27-
return hexBuilder.toString().toLowerCase();
27+
return removeLeadingZeros(hexBuilder.toString().toLowerCase());
28+
}
29+
30+
private static String removeLeadingZeros(String str) {
31+
if (str == null || str.isEmpty()) {
32+
return str;
33+
}
34+
35+
int i = 0;
36+
while (i < str.length() && str.charAt(i) == '0') {
37+
i++;
38+
}
39+
40+
return i == str.length() ? "0" : str.substring(i);
2841
}
2942
}

src/test/java/com/thealgorithms/conversions/DecimalToHexadecimalTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
public class DecimalToHexadecimalTest {
99
@ParameterizedTest
10-
@CsvSource({"0, 00000000", "1, 00000001", "10, 0000000a", "15, 0000000f", "16, 00000010", "255, 000000ff", "190, 000000be", "1800, 00000708"})
10+
@CsvSource({"0, 0", "1, 1", "10, a", "15, f", "16, 10", "255, ff", "190, be", "1800, 708"})
1111
void testDecToHex(int decimal, String expectedHex) {
1212
assertEquals(expectedHex, DecimalToHexadecimal.decToHex(decimal));
1313
}

0 commit comments

Comments
 (0)