From ccf506514a6bfdbbf7f1b7a553dce88a107bcfb3 Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 02:24:06 +0100 Subject: [PATCH 1/4] Add HexadecimalToDecimal conversion --- .../com/conversions/HexadecimalToDecimal.java | 37 +++++++++++++++++++ .../conversions/HexadecimalToDecimalTest.java | 23 ++++++++++++ 2 files changed, 60 insertions(+) create mode 100644 src/main/java/com/conversions/HexadecimalToDecimal.java create mode 100644 src/test/java/com/conversions/HexadecimalToDecimalTest.java diff --git a/src/main/java/com/conversions/HexadecimalToDecimal.java b/src/main/java/com/conversions/HexadecimalToDecimal.java new file mode 100644 index 00000000..1e1089a5 --- /dev/null +++ b/src/main/java/com/conversions/HexadecimalToDecimal.java @@ -0,0 +1,37 @@ +package com.conversions; + + +public class HexadecimalToDecimal { + + /** + * This method converts a Hexadecimal number to a decimal number + * + * @param hexadecimalStr + * @return decimal number + */ + public String hexToDecimal(String hexaStr) { + String hexaNumbers = "0123456789ABCDEF"; + int m, result = 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); + + /** + * 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); + return decimalStr ; + } +} diff --git a/src/test/java/com/conversions/HexadecimalToDecimalTest.java b/src/test/java/com/conversions/HexadecimalToDecimalTest.java new file mode 100644 index 00000000..17640309 --- /dev/null +++ b/src/test/java/com/conversions/HexadecimalToDecimalTest.java @@ -0,0 +1,23 @@ +package com.conversions; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class HexadecimalToDecimalTest { + + @Test + void testHexadecimalToDecimalTest() { + + HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal(); + Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion"); + Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion"); + Assertions.assertEquals("5174921", hexadecimalToDecimal.hexToDecimal("4ef689"), "Incorrect Conversion"); + Assertions.assertEquals("1263", hexadecimalToDecimal.hexToDecimal("4EF"), "Incorrect Conversion"); + Assertions.assertEquals("11259375", hexadecimalToDecimal.hexToDecimal("ABCDEF"), "Incorrect Conversion"); + //It returns -1 if you enter a wrong hexaDecimal + Assertions.assertEquals("-1", hexadecimalToDecimal.hexToDecimal("K"), "Incorrect Conversion"); + + } + +} From 724ff7ef3078b114bf8fa180aab246422aff17e0 Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 03:33:41 +0100 Subject: [PATCH 2/4] Add Hexadecimal to binary conversion --- .../com/conversions/HexadecimalToBinary.java | 48 +++++++++++++++++++ .../conversions/HexadecimalToBinaryTest.java | 22 +++++++++ 2 files changed, 70 insertions(+) create mode 100644 src/main/java/com/conversions/HexadecimalToBinary.java create mode 100644 src/test/java/com/conversions/HexadecimalToBinaryTest.java diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java new file mode 100644 index 00000000..c30942ad --- /dev/null +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -0,0 +1,48 @@ +package com.conversions; + +public class HexadecimalToBinary { + /** + * This method converts a hexadecimal number to + * a binary number. + * + * @param hexStr The hexadecimal number + * @return The binary number + */ + + public String hexToBin (String hexStr) { + + String binaryString = "", hexaNumbers = "0123456789ABCDEF"; + int indexOfHex, decimalNumber = 0, k = 1; + char letter; + int binaryArray[] = new int [50]; + + hexStr = hexStr.toUpperCase(); + + /** + * Transform the hexadecimal number to decimal number + */ + for ( int i = 0 ; i < hexStr.length(); i++) { + letter = hexStr.charAt(i); + indexOfHex = hexaNumbers.indexOf(letter); + decimalNumber = 16 * decimalNumber + indexOfHex; + } + + /** + * Transform decimal number to binary and put it in an array + */ + while (decimalNumber != 0) { + binaryArray[k++] = decimalNumber % 2; + decimalNumber = decimalNumber / 2; + } + /** + * Put the binary in a string + */ + for ( int j = k-1 ; j>0 ; j--) { + binaryString = binaryString + binaryArray[j]; + } + + return binaryString; + + } + +} diff --git a/src/test/java/com/conversions/HexadecimalToBinaryTest.java b/src/test/java/com/conversions/HexadecimalToBinaryTest.java new file mode 100644 index 00000000..0a169b86 --- /dev/null +++ b/src/test/java/com/conversions/HexadecimalToBinaryTest.java @@ -0,0 +1,22 @@ +package com.conversions; + +import static org.junit.jupiter.api.Assertions.*; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class HexadecimalToBinaryTest { + + @Test + void test() { + HexadecimalToBinary hexadecimalToBinary = new HexadecimalToBinary(); + Assertions.assertEquals("10101011", hexadecimalToBinary.hexToBin("AB"), "Incorrect Conversion"); + Assertions.assertEquals("10101101010101111001101", hexadecimalToBinary.hexToBin("56ABCD"), "Incorrect Conversion"); + Assertions.assertEquals("10011101111011010001001", hexadecimalToBinary.hexToBin("4ef689"), "Incorrect Conversion"); + Assertions.assertEquals("10011101111", hexadecimalToBinary.hexToBin("4EF"), "Incorrect Conversion"); + Assertions.assertEquals("101010111100110111101111", hexadecimalToBinary.hexToBin("ABCDEF"), "Incorrect Conversion"); + //It returns -1 if you enter a wrong hexaDecimal + Assertions.assertEquals("-1", hexadecimalToBinary.hexToBin("K"), "Incorrect Conversion"); + } + +} From fd24ffc5e35555f7ae0e5eef5f45f00fc7fc4a30 Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 15:03:41 +0100 Subject: [PATCH 3/4] Fix requested changes --- .../com/conversions/HexadecimalToBinary.java | 96 +++++++++++++++---- .../conversions/HexadecimalToBinaryTest.java | 14 ++- 2 files changed, 88 insertions(+), 22 deletions(-) diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java index c30942ad..6758606e 100644 --- a/src/main/java/com/conversions/HexadecimalToBinary.java +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -11,34 +11,92 @@ public class HexadecimalToBinary { public String hexToBin (String hexStr) { - String binaryString = "", hexaNumbers = "0123456789ABCDEF"; - int indexOfHex, decimalNumber = 0, k = 1; + String binaryString = "", hexaNumbers = "0123456789ABCDEF", + DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; + int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0 + , decimalNumberAfter = 0; char letter; - int binaryArray[] = new int [50]; + int binaryArray[] = new int [60]; + int binaryArrayBefore[] = new int [60]; + int binaryArrayAfter[] = new int [60]; hexStr = hexStr.toUpperCase(); - + int pointPosition = hexStr.indexOf("."); /** * Transform the hexadecimal number to decimal number */ + if ( pointPosition == -1) { for ( int i = 0 ; i < hexStr.length(); i++) { - letter = hexStr.charAt(i); - indexOfHex = hexaNumbers.indexOf(letter); - decimalNumber = 16 * decimalNumber + indexOfHex; + letter = hexStr.charAt(i); + indexOfHex = hexaNumbers.indexOf(letter); + decimalNumber = 16 * decimalNumber + indexOfHex; + } + } + else { + for ( int i = 0 ; i < pointPosition ; i++) { + letter = hexStr.charAt(i); + indexOfHex = hexaNumbers.indexOf(letter); + decimalNumberBefore = 16 * decimalNumberBefore + indexOfHex; + } + String decimalNumberBeforeStr = String.valueOf(decimalNumberBefore); + + for ( int i = pointPosition+1 ; i < hexStr.length() ; i++) { + letter = hexStr.charAt(i); + indexOfHex = hexaNumbers.indexOf(letter); + decimalNumberAfter = 16 * decimalNumberAfter + indexOfHex; + } + + String decimalNumberAfterStr = String.valueOf(decimalNumberAfter); + + DecimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr; + } + + + + int pointPositionDec = DecimalStr.indexOf("."); + /** + * Check whether the result contains a floating point or not + */ + if (pointPositionDec == -1) { + while (decimalNumber != 0) { + binaryArray[k++] = decimalNumber % 2; + decimalNumber = decimalNumber / 2; + } + + }else { + /** + * If it contains floating points we need to divide it into two parts before the point and after it + */ + while (decimalNumberBefore != 0) { + binaryArrayBefore[z++] = decimalNumberBefore % 2; + decimalNumberBefore = decimalNumberBefore / 2; + } + while (decimalNumberAfter != 0) { + binaryArrayAfter[n++] = decimalNumberAfter % 2; + decimalNumberAfter = decimalNumberAfter / 2; + } + } - /** - * Transform decimal number to binary and put it in an array - */ - while (decimalNumber != 0) { - binaryArray[k++] = decimalNumber % 2; - decimalNumber = decimalNumber / 2; - } - /** - * Put the binary in a string - */ - for ( int j = k-1 ; j>0 ; j--) { - binaryString = binaryString + binaryArray[j]; + if(pointPositionDec == -1) { + for ( int j = k-1 ; j>0 ; j--) { + binaryString = binaryString + binaryArray[j]; + } + }else { + for ( int j = z-1 ; j>0 ; j--) { + binaryStringBefore = binaryStringBefore + binaryArrayBefore[j]; + } + + for ( int j = n-1 ; j>0 ; j--) { + binaryStringAfter = binaryStringAfter + binaryArrayAfter[j]; + } + /** + * Remove the zeroes in the end of the hexadecimal + */ + binaryStringAfter = binaryStringAfter.replaceAll("0*$", "").replaceAll("\\.$", ""); + + + binaryString = binaryStringBefore + "." + binaryStringAfter; } return binaryString; diff --git a/src/test/java/com/conversions/HexadecimalToBinaryTest.java b/src/test/java/com/conversions/HexadecimalToBinaryTest.java index 0a169b86..676ac39a 100644 --- a/src/test/java/com/conversions/HexadecimalToBinaryTest.java +++ b/src/test/java/com/conversions/HexadecimalToBinaryTest.java @@ -1,7 +1,5 @@ package com.conversions; -import static org.junit.jupiter.api.Assertions.*; - import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; @@ -9,14 +7,24 @@ class HexadecimalToBinaryTest { @Test void test() { + //HexadecimaltTesting HexadecimalToBinary hexadecimalToBinary = new HexadecimalToBinary(); - Assertions.assertEquals("10101011", hexadecimalToBinary.hexToBin("AB"), "Incorrect Conversion"); + Assertions.assertEquals("1011110011101111", hexadecimalToBinary.hexToBin("BCEF"), "Incorrect Conversion"); Assertions.assertEquals("10101101010101111001101", hexadecimalToBinary.hexToBin("56ABCD"), "Incorrect Conversion"); Assertions.assertEquals("10011101111011010001001", hexadecimalToBinary.hexToBin("4ef689"), "Incorrect Conversion"); Assertions.assertEquals("10011101111", hexadecimalToBinary.hexToBin("4EF"), "Incorrect Conversion"); Assertions.assertEquals("101010111100110111101111", hexadecimalToBinary.hexToBin("ABCDEF"), "Incorrect Conversion"); //It returns -1 if you enter a wrong hexaDecimal Assertions.assertEquals("-1", hexadecimalToBinary.hexToBin("K"), "Incorrect Conversion"); + + + //Hexadecimal with floating point testing + Assertions.assertEquals("101010111100.101111", hexadecimalToBinary.hexToBin("ABC.BC"), "Incorrect Conversion"); + Assertions.assertEquals("10101101010.101111001101", hexadecimalToBinary.hexToBin("56A.BCD"), "Incorrect Conversion"); + Assertions.assertEquals("1001110.1111011010001001", hexadecimalToBinary.hexToBin("4e.f689"), "Incorrect Conversion"); + Assertions.assertEquals("1001110.1111", hexadecimalToBinary.hexToBin("4E.F"), "Incorrect Conversion"); + Assertions.assertEquals("10101011110011011110.1111", hexadecimalToBinary.hexToBin("ABCDE.F"), "Incorrect Conversion"); + } } From 9ee9612b43e1398fc011b64dfbefe1e98c89237e Mon Sep 17 00:00:00 2001 From: MohamedBechir Date: Mon, 25 May 2020 20:39:57 +0100 Subject: [PATCH 4/4] 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"); + } }