diff --git a/src/main/java/com/conversions/HexadecimalToBinary.java b/src/main/java/com/conversions/HexadecimalToBinary.java new file mode 100644 index 00000000..6758606e --- /dev/null +++ b/src/main/java/com/conversions/HexadecimalToBinary.java @@ -0,0 +1,106 @@ +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", + DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = ""; + int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0 + , decimalNumberAfter = 0; + char letter; + 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; + } + } + 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; + } + + } + + 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/main/java/com/conversions/HexadecimalToDecimal.java b/src/main/java/com/conversions/HexadecimalToDecimal.java new file mode 100644 index 00000000..57e54ac2 --- /dev/null +++ b/src/main/java/com/conversions/HexadecimalToDecimal.java @@ -0,0 +1,68 @@ +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, 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 + * 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); + + } + 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 ; + } +} diff --git a/src/test/java/com/conversions/HexadecimalToBinaryTest.java b/src/test/java/com/conversions/HexadecimalToBinaryTest.java new file mode 100644 index 00000000..676ac39a --- /dev/null +++ b/src/test/java/com/conversions/HexadecimalToBinaryTest.java @@ -0,0 +1,30 @@ +package com.conversions; + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class HexadecimalToBinaryTest { + + @Test + void test() { + //HexadecimaltTesting + HexadecimalToBinary hexadecimalToBinary = new HexadecimalToBinary(); + 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"); + + } + +} diff --git a/src/test/java/com/conversions/HexadecimalToDecimalTest.java b/src/test/java/com/conversions/HexadecimalToDecimalTest.java new file mode 100644 index 00000000..c7d4ee3c --- /dev/null +++ b/src/test/java/com/conversions/HexadecimalToDecimalTest.java @@ -0,0 +1,32 @@ +package com.conversions; + + +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.Test; + +class HexadecimalToDecimalTest { + + @Test + 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"); + 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"); + + //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"); + + } + +}