From 9ee9612b43e1398fc011b64dfbefe1e98c89237e Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 20:39:57 +0100 Subject: [PATCH] Take into consideration hexadecimal numbers with floating point --- .../com/conversions/HexadecimalToDecimal.java | 65 ++++++++++++++----- .../conversions/HexadecimalToDecimalTest.java | 9 +++ 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/src/main/java/com/conversions/HexadecimalToDecimal.java b/src/main/java/com/conversions/HexadecimalToDecimal.java index 1e1089a5..57e54ac2 100644 --- a/src/main/java/com/conversions/HexadecimalToDecimal.java +++ b/src/main/java/com/conversions/HexadecimalToDecimal.java @@ -11,27 +11,58 @@ 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(); - - for (int i = 0 ; i < hexaStr.length() ; i++) { - /** - * Letter will store the hexadecimal number as long as we loop through - * the string - */ - letter = hexaStr.charAt(i); + 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 + * the string + */ + letter = hexaStr.charAt(i); - /** - * m is the index of the number that we are looping through in the - * hexaNumbers - */ - m = hexaNumbers.indexOf(letter); - result = 16*result + m; - } + /** + * m is the index of the number that we are looping through in the + * hexaNumbers + */ + 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; + } - decimalStr = String.valueOf(result); - return decimalStr ; + return decimalStr ; } } diff --git a/src/test/java/com/conversions/HexadecimalToDecimalTest.java b/src/test/java/com/conversions/HexadecimalToDecimalTest.java index 17640309..c7d4ee3c 100644 --- a/src/test/java/com/conversions/HexadecimalToDecimalTest.java +++ b/src/test/java/com/conversions/HexadecimalToDecimalTest.java @@ -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"); + } }