Skip to content

Commit 9f8abb0

Browse files
authored
Merge pull request TheAlgorithms#1328 from MohamedBechir/Development
Add hexadecimal to binary and hexadecimal to decimal conversions
2 parents 8426bed + 9ee9612 commit 9f8abb0

File tree

4 files changed

+236
-0
lines changed

4 files changed

+236
-0
lines changed
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
package com.conversions;
2+
3+
public class HexadecimalToBinary {
4+
/**
5+
* This method converts a hexadecimal number to
6+
* a binary number.
7+
*
8+
* @param hexStr The hexadecimal number
9+
* @return The binary number
10+
*/
11+
12+
public String hexToBin (String hexStr) {
13+
14+
String binaryString = "", hexaNumbers = "0123456789ABCDEF",
15+
DecimalStr ="", binaryStringBefore ="" , binaryStringAfter = "";
16+
int indexOfHex, decimalNumber = 0, k = 1, n =1, z=1, decimalNumberBefore = 0
17+
, decimalNumberAfter = 0;
18+
char letter;
19+
int binaryArray[] = new int [60];
20+
int binaryArrayBefore[] = new int [60];
21+
int binaryArrayAfter[] = new int [60];
22+
23+
hexStr = hexStr.toUpperCase();
24+
int pointPosition = hexStr.indexOf(".");
25+
/**
26+
* Transform the hexadecimal number to decimal number
27+
*/
28+
if ( pointPosition == -1) {
29+
for ( int i = 0 ; i < hexStr.length(); i++) {
30+
letter = hexStr.charAt(i);
31+
indexOfHex = hexaNumbers.indexOf(letter);
32+
decimalNumber = 16 * decimalNumber + indexOfHex;
33+
}
34+
}
35+
else {
36+
for ( int i = 0 ; i < pointPosition ; i++) {
37+
letter = hexStr.charAt(i);
38+
indexOfHex = hexaNumbers.indexOf(letter);
39+
decimalNumberBefore = 16 * decimalNumberBefore + indexOfHex;
40+
}
41+
String decimalNumberBeforeStr = String.valueOf(decimalNumberBefore);
42+
43+
for ( int i = pointPosition+1 ; i < hexStr.length() ; i++) {
44+
letter = hexStr.charAt(i);
45+
indexOfHex = hexaNumbers.indexOf(letter);
46+
decimalNumberAfter = 16 * decimalNumberAfter + indexOfHex;
47+
}
48+
49+
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
50+
51+
DecimalStr = decimalNumberBeforeStr + '.' + decimalNumberAfterStr;
52+
}
53+
54+
55+
56+
int pointPositionDec = DecimalStr.indexOf(".");
57+
/**
58+
* Check whether the result contains a floating point or not
59+
*/
60+
if (pointPositionDec == -1) {
61+
while (decimalNumber != 0) {
62+
binaryArray[k++] = decimalNumber % 2;
63+
decimalNumber = decimalNumber / 2;
64+
}
65+
66+
}else {
67+
/**
68+
* If it contains floating points we need to divide it into two parts before the point and after it
69+
*/
70+
while (decimalNumberBefore != 0) {
71+
binaryArrayBefore[z++] = decimalNumberBefore % 2;
72+
decimalNumberBefore = decimalNumberBefore / 2;
73+
}
74+
while (decimalNumberAfter != 0) {
75+
binaryArrayAfter[n++] = decimalNumberAfter % 2;
76+
decimalNumberAfter = decimalNumberAfter / 2;
77+
}
78+
79+
}
80+
81+
if(pointPositionDec == -1) {
82+
for ( int j = k-1 ; j>0 ; j--) {
83+
binaryString = binaryString + binaryArray[j];
84+
}
85+
}else {
86+
for ( int j = z-1 ; j>0 ; j--) {
87+
binaryStringBefore = binaryStringBefore + binaryArrayBefore[j];
88+
}
89+
90+
for ( int j = n-1 ; j>0 ; j--) {
91+
binaryStringAfter = binaryStringAfter + binaryArrayAfter[j];
92+
}
93+
/**
94+
* Remove the zeroes in the end of the hexadecimal
95+
*/
96+
binaryStringAfter = binaryStringAfter.replaceAll("0*$", "").replaceAll("\\.$", "");
97+
98+
99+
binaryString = binaryStringBefore + "." + binaryStringAfter;
100+
}
101+
102+
return binaryString;
103+
104+
}
105+
106+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package com.conversions;
2+
3+
4+
public class HexadecimalToDecimal {
5+
6+
/**
7+
* This method converts a Hexadecimal number to a decimal number
8+
*
9+
* @param hexadecimalStr
10+
* @return decimal number
11+
*/
12+
public String hexToDecimal(String hexaStr) {
13+
String hexaNumbers = "0123456789ABCDEF";
14+
int m, result = 0, decimalNumberBefore = 0, power = -1;
15+
Double decimalNumberAfter = 0.0;
16+
char letter;
17+
String decimalStr;
18+
hexaStr = hexaStr.toUpperCase();
19+
int pointPosition = hexaStr.indexOf(".");
20+
/**
21+
* Check whether the number contains a float point or not
22+
*/
23+
if ( pointPosition == -1) {
24+
for (int i = 0 ; i < hexaStr.length() ; i++) {
25+
/**
26+
* Letter will store the hexadecimal number as long as we loop through
27+
* the string
28+
*/
29+
letter = hexaStr.charAt(i);
30+
31+
/**
32+
* m is the index of the number that we are looping through in the
33+
* hexaNumbers
34+
*/
35+
m = hexaNumbers.indexOf(letter);
36+
result = 16*result + m;
37+
}
38+
decimalStr = String.valueOf(result);
39+
40+
}
41+
else {
42+
for ( int i = 0 ; i < pointPosition ; i++) {
43+
letter = hexaStr.charAt(i);
44+
m = hexaNumbers.indexOf(letter);
45+
decimalNumberBefore = 16*decimalNumberBefore + m;
46+
}
47+
48+
String decimalNumberBeforeStr = String.valueOf(decimalNumberBefore);
49+
50+
for ( int i = pointPosition+1 ; i < hexaStr.length() ; i++) {
51+
letter = hexaStr.charAt(i);
52+
m = hexaNumbers.indexOf(letter);
53+
decimalNumberAfter = (decimalNumberAfter + (Math.pow(16, power))*m);
54+
power = power-1;
55+
}
56+
/**
57+
* Retrieve the decimal part of the result
58+
*/
59+
String decimalNumberAfterStr = String.valueOf(decimalNumberAfter);
60+
int indexOfDecimal = decimalNumberAfterStr.indexOf(".");
61+
decimalNumberAfterStr = decimalNumberAfterStr.substring(indexOfDecimal);
62+
63+
decimalStr = decimalNumberBeforeStr + decimalNumberAfterStr;
64+
}
65+
66+
return decimalStr ;
67+
}
68+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.conversions;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
class HexadecimalToBinaryTest {
7+
8+
@Test
9+
void test() {
10+
//HexadecimaltTesting
11+
HexadecimalToBinary hexadecimalToBinary = new HexadecimalToBinary();
12+
Assertions.assertEquals("1011110011101111", hexadecimalToBinary.hexToBin("BCEF"), "Incorrect Conversion");
13+
Assertions.assertEquals("10101101010101111001101", hexadecimalToBinary.hexToBin("56ABCD"), "Incorrect Conversion");
14+
Assertions.assertEquals("10011101111011010001001", hexadecimalToBinary.hexToBin("4ef689"), "Incorrect Conversion");
15+
Assertions.assertEquals("10011101111", hexadecimalToBinary.hexToBin("4EF"), "Incorrect Conversion");
16+
Assertions.assertEquals("101010111100110111101111", hexadecimalToBinary.hexToBin("ABCDEF"), "Incorrect Conversion");
17+
//It returns -1 if you enter a wrong hexaDecimal
18+
Assertions.assertEquals("-1", hexadecimalToBinary.hexToBin("K"), "Incorrect Conversion");
19+
20+
21+
//Hexadecimal with floating point testing
22+
Assertions.assertEquals("101010111100.101111", hexadecimalToBinary.hexToBin("ABC.BC"), "Incorrect Conversion");
23+
Assertions.assertEquals("10101101010.101111001101", hexadecimalToBinary.hexToBin("56A.BCD"), "Incorrect Conversion");
24+
Assertions.assertEquals("1001110.1111011010001001", hexadecimalToBinary.hexToBin("4e.f689"), "Incorrect Conversion");
25+
Assertions.assertEquals("1001110.1111", hexadecimalToBinary.hexToBin("4E.F"), "Incorrect Conversion");
26+
Assertions.assertEquals("10101011110011011110.1111", hexadecimalToBinary.hexToBin("ABCDE.F"), "Incorrect Conversion");
27+
28+
}
29+
30+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.conversions;
2+
3+
4+
import org.junit.jupiter.api.Assertions;
5+
import org.junit.jupiter.api.Test;
6+
7+
class HexadecimalToDecimalTest {
8+
9+
@Test
10+
void testHexadecimalToDecimalTest() {
11+
12+
HexadecimalToDecimal hexadecimalToDecimal = new HexadecimalToDecimal();
13+
14+
//HexadecimaltTesting
15+
Assertions.assertEquals("171", hexadecimalToDecimal.hexToDecimal("AB"), "Incorrect Conversion");
16+
Assertions.assertEquals("5680077", hexadecimalToDecimal.hexToDecimal("56ABCD"), "Incorrect Conversion");
17+
Assertions.assertEquals("5174921", hexadecimalToDecimal.hexToDecimal("4ef689"), "Incorrect Conversion");
18+
Assertions.assertEquals("1263", hexadecimalToDecimal.hexToDecimal("4EF"), "Incorrect Conversion");
19+
Assertions.assertEquals("11259375", hexadecimalToDecimal.hexToDecimal("ABCDEF"), "Incorrect Conversion");
20+
//It returns -1 if you enter a wrong hexaDecimal
21+
Assertions.assertEquals("-1", hexadecimalToDecimal.hexToDecimal("K"), "Incorrect Conversion");
22+
23+
//Hexadecimal with floating point testing
24+
Assertions.assertEquals("10.6875", hexadecimalToDecimal.hexToDecimal("A.B"), "Incorrect Conversion");
25+
Assertions.assertEquals("1386.737548828125", hexadecimalToDecimal.hexToDecimal("56A.BCD"), "Incorrect Conversion");
26+
Assertions.assertEquals("78.9630279541015625", hexadecimalToDecimal.hexToDecimal("4e.f689"), "Incorrect Conversion");
27+
Assertions.assertEquals("0.93359375", hexadecimalToDecimal.hexToDecimal(".EF"), "Incorrect Conversion");
28+
Assertions.assertEquals("171.8044281005859375", hexadecimalToDecimal.hexToDecimal("AB.CDEF"), "Incorrect Conversion");
29+
30+
}
31+
32+
}

0 commit comments

Comments
 (0)