Skip to content

Commit 9ee9612

Browse files
committed
Take into consideration hexadecimal numbers with floating point
1 parent fd24ffc commit 9ee9612

File tree

2 files changed

+57
-17
lines changed

2 files changed

+57
-17
lines changed

src/main/java/com/conversions/HexadecimalToDecimal.java

Lines changed: 48 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,27 +11,58 @@ public class HexadecimalToDecimal {
1111
*/
1212
public String hexToDecimal(String hexaStr) {
1313
String hexaNumbers = "0123456789ABCDEF";
14-
int m, result = 0;
14+
int m, result = 0, decimalNumberBefore = 0, power = -1;
15+
Double decimalNumberAfter = 0.0;
1516
char letter;
1617
String decimalStr;
1718
hexaStr = hexaStr.toUpperCase();
18-
19-
for (int i = 0 ; i < hexaStr.length() ; i++) {
20-
/**
21-
* Letter will store the hexadecimal number as long as we loop through
22-
* the string
23-
*/
24-
letter = hexaStr.charAt(i);
19+
int pointPosition = hexaStr.indexOf(".");
20+
/**
21+
* Check whether the number contains a float point or not
22+
*/
23+
if ( pointPosition == -1) {
24+
for (int i = 0 ; i < hexaStr.length() ; i++) {
25+
/**
26+
* Letter will store the hexadecimal number as long as we loop through
27+
* the string
28+
*/
29+
letter = hexaStr.charAt(i);
2530

26-
/**
27-
* m is the index of the number that we are looping through in the
28-
* hexaNumbers
29-
*/
30-
m = hexaNumbers.indexOf(letter);
31-
result = 16*result + m;
32-
}
31+
/**
32+
* m is the index of the number that we are looping through in the
33+
* hexaNumbers
34+
*/
35+
m = hexaNumbers.indexOf(letter);
36+
result = 16*result + m;
37+
}
38+
decimalStr = String.valueOf(result);
39+
40+
}
41+
else {
42+
for ( int i = 0 ; i < pointPosition ; i++) {
43+
letter = hexaStr.charAt(i);
44+
m = hexaNumbers.indexOf(letter);
45+
decimalNumberBefore = 16*decimalNumberBefore + m;
46+
}
47+
48+
String decimalNumberBeforeStr = String.valueOf(decimalNumberBefore);
49+
50+
for ( int i = pointPosition+1 ; i < hexaStr.length() ; i++) {
51+
letter = hexaStr.charAt(i);
52+
m = hexaNumbers.indexOf(letter);
53+
decimalNumberAfter = (decimalNumberAfter + (Math.pow(16, power))*m);
54+
power = power-1;
55+
}
56+
/**
57+
* Retrieve the decimal part of the result
58+
*/
59+
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
60+
int indexOfDecimal = decimalNumberAfterStr.indexOf(".");
61+
decimalNumberAfterStr = decimalNumberAfterStr.substring(indexOfDecimal);
62+
63+
decimalStr = decimalNumberBeforeStr + decimalNumberAfterStr;
64+
}
3365

34-
decimalStr = String.valueOf(result);
35-
return decimalStr ;
66+
return decimalStr ;
3667
}
3768
}

src/test/java/com/conversions/HexadecimalToDecimalTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ class HexadecimalToDecimalTest {
1010
void testHexadecimalToDecimalTest() {
1111

1212
HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal();
13+
14+
//HexadecimaltTesting
1315
Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion");
1416
Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion");
1517
Assertions.assertEquals("5174921", hexadecimalToDecimal.hexToDecimal("4ef689"), "Incorrect Conversion");
@@ -18,6 +20,13 @@ void testHexadecimalToDecimalTest() {
1820
//It returns -1 if you enter a wrong hexaDecimal
1921
Assertions.assertEquals("-1", hexadecimalToDecimal.hexToDecimal("K"), "Incorrect Conversion");
2022

23+
//Hexadecimal with floating point testing
24+
Assertions.assertEquals("10.6875", hexadecimalToDecimal.hexToDecimal("A.B"), "Incorrect Conversion");
25+
Assertions.assertEquals("1386.737548828125", hexadecimalToDecimal.hexToDecimal("56A.BCD"), "Incorrect Conversion");
26+
Assertions.assertEquals("78.9630279541015625", hexadecimalToDecimal.hexToDecimal("4e.f689"), "Incorrect Conversion");
27+
Assertions.assertEquals("0.93359375", hexadecimalToDecimal.hexToDecimal(".EF"), "Incorrect Conversion");
28+
Assertions.assertEquals("171.8044281005859375", hexadecimalToDecimal.hexToDecimal("AB.CDEF"), "Incorrect Conversion");
29+
2130
}
2231

2332
}

0 commit comments

Comments
 (0)