Take into consideration hexadecimal numbers with floating point
This commit is contained in:
parent
fd24ffc5e3
commit
9ee9612b43
@ -11,27 +11,58 @@ public class HexadecimalToDecimal {
|
|||||||
*/
|
*/
|
||||||
public String hexToDecimal(String hexaStr) {
|
public String hexToDecimal(String hexaStr) {
|
||||||
String hexaNumbers = "0123456789ABCDEF";
|
String hexaNumbers = "0123456789ABCDEF";
|
||||||
int m, result = 0;
|
int m, result = 0, decimalNumberBefore = 0, power = -1;
|
||||||
|
Double decimalNumberAfter = 0.0;
|
||||||
char letter;
|
char letter;
|
||||||
String decimalStr;
|
String decimalStr;
|
||||||
hexaStr = hexaStr.toUpperCase();
|
hexaStr = hexaStr.toUpperCase();
|
||||||
|
int pointPosition = hexaStr.indexOf(".");
|
||||||
for (int i = 0 ; i < hexaStr.length() ; i++) {
|
/**
|
||||||
/**
|
* Check whether the number contains a float point or not
|
||||||
* Letter will store the hexadecimal number as long as we loop through
|
*/
|
||||||
* the string
|
if ( pointPosition == -1) {
|
||||||
*/
|
for (int i = 0 ; i < hexaStr.length() ; i++) {
|
||||||
letter = hexaStr.charAt(i);
|
/**
|
||||||
|
* Letter will store the hexadecimal number as long as we loop through
|
||||||
|
* the string
|
||||||
|
*/
|
||||||
|
letter = hexaStr.charAt(i);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* m is the index of the number that we are looping through in the
|
* m is the index of the number that we are looping through in the
|
||||||
* hexaNumbers
|
* hexaNumbers
|
||||||
*/
|
*/
|
||||||
m = hexaNumbers.indexOf(letter);
|
m = hexaNumbers.indexOf(letter);
|
||||||
result = 16*result + m;
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
decimalStr = String.valueOf(result);
|
return decimalStr ;
|
||||||
return decimalStr ;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -10,6 +10,8 @@ class HexadecimalToDecimalTest {
|
|||||||
void testHexadecimalToDecimalTest() {
|
void testHexadecimalToDecimalTest() {
|
||||||
|
|
||||||
HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal();
|
HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal();
|
||||||
|
|
||||||
|
//HexadecimaltTesting
|
||||||
Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion");
|
Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion");
|
||||||
Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion");
|
Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion");
|
||||||
Assertions.assertEquals("5174921", hexadecimalToDecimal.hexToDecimal("4ef689"), "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
|
//It returns -1 if you enter a wrong hexaDecimal
|
||||||
Assertions.assertEquals("-1", hexadecimalToDecimal.hexToDecimal("K"), "Incorrect Conversion");
|
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");
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user