Skip to content

Commit c20f7c2

Browse files
authored
Merge branch 'master' into matrix_graphs_add_tests
2 parents ced3dec + a8b8420 commit c20f7c2

File tree

2 files changed

+56
-27
lines changed

2 files changed

+56
-27
lines changed
Lines changed: 28 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,45 @@
11
package com.thealgorithms.conversions;
22

3-
import java.util.Scanner;
4-
3+
/**
4+
* Utility class for converting a hexadecimal string to its decimal representation.
5+
* <p>
6+
* A hexadecimal number uses the base-16 numeral system, with the following characters:
7+
* <ul>
8+
* <li>Digits: 0-9</li>
9+
* <li>Letters: A-F (case-insensitive)</li>
10+
* </ul>
11+
* Each character represents a power of 16. For example:
12+
* <pre>
13+
* Hexadecimal "A1" = 10*16^1 + 1*16^0 = 161 (decimal)
14+
* </pre>
15+
*
16+
* <p>This class provides a method to perform the conversion without using built-in Java utilities.</p>
17+
*/
518
public final class HexaDecimalToDecimal {
619
private HexaDecimalToDecimal() {
720
}
821

9-
// convert hexadecimal to decimal
22+
/**
23+
* Converts a hexadecimal string to its decimal integer equivalent.
24+
* <p>The input string is case-insensitive, and must contain valid hexadecimal characters [0-9, A-F].</p>
25+
*
26+
* @param hex the hexadecimal string to convert
27+
* @return the decimal integer representation of the input hexadecimal string
28+
* @throws IllegalArgumentException if the input string contains invalid characters
29+
*/
1030
public static int getHexaToDec(String hex) {
1131
String digits = "0123456789ABCDEF";
1232
hex = hex.toUpperCase();
1333
int val = 0;
34+
1435
for (int i = 0; i < hex.length(); i++) {
1536
int d = digits.indexOf(hex.charAt(i));
37+
if (d == -1) {
38+
throw new IllegalArgumentException("Invalid hexadecimal character: " + hex.charAt(i));
39+
}
1640
val = 16 * val + d;
1741
}
18-
return val;
19-
}
2042

21-
// Main method gets the hexadecimal input from user and converts it into Decimal output.
22-
public static void main(String[] args) {
23-
String hexaInput;
24-
int decOutput;
25-
Scanner scan = new Scanner(System.in);
26-
27-
System.out.print("Enter Hexadecimal Number : ");
28-
hexaInput = scan.nextLine();
29-
30-
// convert hexadecimal to decimal
31-
decOutput = getHexaToDec(hexaInput);
32-
/*
33-
Pass the string to the getHexaToDec function
34-
and it returns the decimal form in the variable decOutput.
35-
*/
36-
System.out.println("Number in Decimal: " + decOutput);
37-
scan.close();
43+
return val;
3844
}
3945
}
Lines changed: 28 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,37 @@
11
package com.thealgorithms.conversions;
22

33
import static org.junit.jupiter.api.Assertions.assertEquals;
4+
import static org.junit.jupiter.api.Assertions.assertThrows;
45

5-
import org.junit.jupiter.api.Test;
6+
import org.junit.jupiter.params.ParameterizedTest;
7+
import org.junit.jupiter.params.provider.CsvSource;
68

79
public class HexaDecimalToDecimalTest {
810

9-
@Test
10-
public void testhexaDecimalToDecimal() {
11-
assertEquals(161, HexaDecimalToDecimal.getHexaToDec("A1"));
12-
assertEquals(428, HexaDecimalToDecimal.getHexaToDec("1ac"));
11+
@ParameterizedTest
12+
@CsvSource({
13+
"A1, 161", // Simple case with two characters
14+
"1AC, 428", // Mixed-case input
15+
"0, 0", // Single zero
16+
"F, 15", // Single digit
17+
"10, 16", // Power of 16
18+
"FFFF, 65535", // Max 4-character hex
19+
"7FFFFFFF, 2147483647" // Max positive int value
20+
})
21+
public void
22+
testValidHexaToDecimal(String hexInput, int expectedDecimal) {
23+
assertEquals(expectedDecimal, HexaDecimalToDecimal.getHexaToDec(hexInput));
24+
}
25+
26+
@ParameterizedTest
27+
@CsvSource({
28+
"G", // Invalid character
29+
"1Z", // Mixed invalid input
30+
"123G", // Valid prefix with invalid character
31+
"#$%" // Non-hexadecimal symbols
32+
})
33+
public void
34+
testInvalidHexaToDecimal(String invalidHex) {
35+
assertThrows(IllegalArgumentException.class, () -> HexaDecimalToDecimal.getHexaToDec(invalidHex));
1336
}
1437
}

0 commit comments

Comments
 (0)