forked from TheAlgorithms/Java
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexadecimalToBinary.java
More file actions
106 lines (87 loc) · 2.92 KB
/
HexadecimalToBinary.java
File metadata and controls
106 lines (87 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
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;
}
}