Merge pull request #1328 from MohamedBechir/Development

Add hexadecimal to binary and hexadecimal  to decimal conversions
This commit is contained in:
Sombit Bose 2020-05-26 01:16:22 +05:30 committed by GitHub
commit 9f8abb0f55
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 236 additions and 0 deletions

View File

@ -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;
}
}

View File

@ -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 ;
}
}

View File

@ -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");
}
}

View File

@ -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");
}
}