Skip to content

Add hexadecimal to binary and hexadecimal to decimal conversions #1328

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 25, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
106 changes: 106 additions & 0 deletions src/main/java/com/conversions/HexadecimalToBinary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
package com.conversions;

public class HexadecimalToBinary {
/**
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code fails in case of floating point conversion, 'ABC.BC' try doing it for real values also

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good work but you missed out in the 2nd file.

* 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;

}

}
68 changes: 68 additions & 0 deletions src/main/java/com/conversions/HexadecimalToDecimal.java
Original file line number Diff line number Diff line change
@@ -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";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your code fails in case of "AB.CD"

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because from the point you must multiply for 1/16, the potency start to be negative

Copy link
Contributor Author

@MohamedBechir MohamedBechir May 26, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nippur101
Yes, that's what I did, thank you

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 ;
}
}
30 changes: 30 additions & 0 deletions src/test/java/com/conversions/HexadecimalToBinaryTest.java
Original file line number Diff line number Diff line change
@@ -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");

}

}
32 changes: 32 additions & 0 deletions src/test/java/com/conversions/HexadecimalToDecimalTest.java
Original file line number Diff line number Diff line change
@@ -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");

}

}