Take into consideration hexadecimal numbers with floating point

This commit is contained in:
MohamedBechir 2020-05-25 20:39:57 +01:00
parent fd24ffc5e3
commit 9ee9612b43
2 changed files with 57 additions and 17 deletions

View File

@ -11,11 +11,16 @@ public class HexadecimalToDecimal {
*/
public String hexToDecimal(String hexaStr) {
String hexaNumbers = "0123456789ABCDEF";
int m, result = 0;
int m, result = 0, decimalNumberBefore = 0, power = -1;
Double decimalNumberAfter = 0.0;
char letter;
String decimalStr;
hexaStr = hexaStr.toUpperCase();
int pointPosition = hexaStr.indexOf(".");
/**
* Check whether the number contains a float point or not
*/
if ( pointPosition == -1) {
for (int i = 0 ; i < hexaStr.length() ; i++) {
/**
* Letter will store the hexadecimal number as long as we loop through
@ -30,8 +35,34 @@ public class HexadecimalToDecimal {
m = hexaNumbers.indexOf(letter);
result = 16*result + m;
}
decimalStr = String.valueOf(result);
}
else {
for ( int i = 0 ; i < pointPosition ; i++) {
letter = hexaStr.charAt(i);
m = hexaNumbers.indexOf(letter);
decimalNumberBefore = 16*decimalNumberBefore + m;
}
String decimalNumberBeforeStr = String.valueOf(decimalNumberBefore);
for ( int i = pointPosition+1 ; i < hexaStr.length() ; i++) {
letter = hexaStr.charAt(i);
m = hexaNumbers.indexOf(letter);
decimalNumberAfter = (decimalNumberAfter + (Math.pow(16, power))*m);
power = power-1;
}
/**
* Retrieve the decimal part of the result
*/
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
int indexOfDecimal = decimalNumberAfterStr.indexOf(".");
decimalNumberAfterStr = decimalNumberAfterStr.substring(indexOfDecimal);
decimalStr = decimalNumberBeforeStr + decimalNumberAfterStr;
}
return decimalStr ;
}
}

View File

@ -10,6 +10,8 @@ class HexadecimalToDecimalTest {
void testHexadecimalToDecimalTest() {
HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal();
//HexadecimaltTesting
Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion");
Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion");
Assertions.assertEquals("5174921", hexadecimalToDecimal.hexToDecimal("4ef689"), "Incorrect Conversion");
@ -18,6 +20,13 @@ class HexadecimalToDecimalTest {
//It returns -1 if you enter a wrong hexaDecimal
Assertions.assertEquals("-1", hexadecimalToDecimal.hexToDecimal("K"), "Incorrect Conversion");
//Hexadecimal with floating point testing
Assertions.assertEquals("10.6875", hexadecimalToDecimal.hexToDecimal("A.B"), "Incorrect Conversion");
Assertions.assertEquals("1386.737548828125", hexadecimalToDecimal.hexToDecimal("56A.BCD"), "Incorrect Conversion");
Assertions.assertEquals("78.9630279541015625", hexadecimalToDecimal.hexToDecimal("4e.f689"), "Incorrect Conversion");
Assertions.assertEquals("0.93359375", hexadecimalToDecimal.hexToDecimal(".EF"), "Incorrect Conversion");
Assertions.assertEquals("171.8044281005859375", hexadecimalToDecimal.hexToDecimal("AB.CDEF"), "Incorrect Conversion");
}
}