-
Notifications
You must be signed in to change notification settings - Fork 0
/
Number_System.html
270 lines (240 loc) · 8.62 KB
/
Number_System.html
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Base Converter | Number System</title>
<style>
body {
font-family: cursive;
font-size: 20px;
background: #7ba4ff36;
}
#heading {
font-size: 30px;
background: #7ba4ff;
padding: 2px 8px;
border-radius: 10px;
text-align: center;
}
.box {
margin-top: 6px;
}
.input-box {
display: block;
margin: auto;
font-size: 18px;
border-radius: 8px;
margin-top: 6px;
padding: 2px 5px;
width: 90%;
}
#error {
text-align: center;
margin: 15px;
background: #7ba4ff;
}
footer {
position: absolute;
bottom: 0;
background: black;
color: white;
left: 0;
padding: 2px 12px;
border-top-right-radius: 15px;
}
#name {
color: #7ba4ff;
}
</style>
</head>
<body>
<h2 id="heading">Base Converter System</h2>
<div id="error"></div>
<form action="">
<div class="box">
<label for="binary">Binary:</label>
<input class="input-box" id="binary-input" type="text" oninput="binaryInputEvent(this.value)"
placeholder="Enter a binary number">
</div>
<div class="box">
<label for="octal">Octal:</label>
<input class="input-box" id="octal-input" type="text" oninput="octalInputEvent(this.value)"
placeholder="Enter an octal number">
</div>
<div class="box">
<label for="decimal">Decimal:</label>
<input class="input-box" id="decimal-input" type="text" oninput="decimalInputEvent(this.value)"
placeholder="Enter a decimal number">
</div>
<div class="box">
<label for="hexa">Hexa-Decimal:</label>
<input class="input-box" id="hexa-input" type="text" oninput="hexaInputEvent(this.value)"
placeholder="Enter a hexa-decimal number">
</div>
</form>
<footer>
<div>Developed By <span id="name">Ali Amir</span></div>
</footer>
<script>
var binaryInput = document.getElementById('binary-input');
var octalInput = document.getElementById('octal-input');
var decimalInput = document.getElementById('decimal-input');
var hexaInput = document.getElementById('hexa-input');
var errorMessageBox = document.getElementById('error');
var errorMessage = '';
function binaryInputEvent(value) {
var isBinary = isValid(value, 2);
if (isBinary) {
errorMessageBox.innerHTML = '';
octalInput.value = binaryToOctal(value);
decimalInput.value = binaryToDecimal(value);
hexaInput.value = binaryToHexa(value);
} else {
octalInput.value = '';
decimalInput.value = '';
hexaInput.value = '';
errorMessageBox.innerHTML = errorMessage;
}
}
function octalInputEvent(value) {
var isOctal = isValid(value, 8);
if (isOctal) {
errorMessageBox.innerHTML = '';
binaryInput.value = ocatlToBinary(value);
decimalInput.value = octalTodecimal(value);
hexaInput.value = octalToHexa(value);
} else {
binaryInput.value = '';
decimalInput.value = '';
hexaInput.value = '';
errorMessageBox.innerHTML = errorMessage;
}
}
function decimalInputEvent(value) {
var isDecimal = isValid(value, 10);
if (isDecimal) {
errorMessageBox.innerHTML = '';
binaryInput.value = decimalToBinary(value);
octalInput.value = decimalToOctal(value);
hexaInput.value = decimalToHexa(value);
} else {
binaryInput.value = '';
octalInput.value = '';
hexaInput.value = '';
errorMessageBox.innerHTML = errorMessage;
}
}
function hexaInputEvent(value) {
value = value.toUpperCase();
var isHexa = isValid(value, 16);
if (isHexa) {
errorMessageBox.innerHTML = '';
binaryInput.value = hexaToBinary(value);
octalInput.value = hexaToOctal(value);
decimalInput.value = hexaTodecimal(value);
} else {
binaryInput.value = '';
octalInput.value = '';
decimalInput.value = '';
errorMessageBox.innerHTML = errorMessage;
}
}
function isValid(number, base) {
var numbers = {
'2': ['0', '1','.'],
'8': ['0', '1', '2', '3', '4', '5', '6', '7','.'],
'10': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9','.'],
'16': ['0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F','.']
}
if (number == '') {
errorMessage = '';
return false;
}
for (var i = 0; i < number.length; i++) {
if (!(numbers[base].includes(number[i]))) {
errorMessage = 'Please enter a valid number for this base.'
return false;
}
}
return true;
}
function binaryToOctal(number) {
return decimalTo(toDecimal(number, 2), 8);
}
function binaryToDecimal(number) {
return toDecimal(number, 2);
}
function binaryToHexa(number) {
return decimalTo(toDecimal(number, 2), 16);
}
function ocatlToBinary(number) {
return decimalTo(toDecimal(number, 8), 2);
}
function octalTodecimal(number) {
return toDecimal(number, 8);
}
function octalToHexa(number) {
return decimalTo(toDecimal(number, 8), 16);
}
function decimalToBinary(number) {
return decimalTo(number, 2);
}
function decimalToOctal(number) {
return decimalTo(number, 8);
}
function decimalToHexa(number) {
return decimalTo(number, 16);
}
function hexaToBinary(number) {
return decimalTo(toDecimal(number, 16), 2);
}
function hexaToOctal(number) {
return decimalTo(toDecimal(number, 16), 8);
}
function hexaTodecimal(number) {
return toDecimal(number, 16);
}
function toDecimal(number, base) {
var arr = number.split('.');
if (!(number.includes('.') || arr[1] == '')) {
return parseInt(number, base).toString(10).toUpperCase();
}
var convertedNumber = parseInt(arr[0], base);
var afterdecimalNumber = 0;
for (var i = 1; i <= arr[1].length; i++) {
afterdecimalNumber += parseInt(arr[1][i - 1]) * Math.pow(base, -i);
}
return (convertedNumber + afterdecimalNumber).toString().toUpperCase();
}
function decimalTo(number, base) {
var arr = number.split('.');
if (!(number.includes('.') || arr[1] == '')) {
return parseInt(number, 10).toString(base).toUpperCase();
}
var convertedNumber = parseInt(arr[0], 10).toString(base);
var afterdecimal = '.' + arr[1];
convertedNumber += afterDecimalMultiply(afterdecimal, base);
return convertedNumber.toUpperCase();
}
function afterDecimalMultiply(afterdecimal, base) {
var number = '.';
for (var i = 0, temp; i < 30; i++) {
temp = (parseFloat(afterdecimal) * base).toString();
temp = temp.split('.');
if (isNaN(temp[0])) {
break;
}
if (base == 16) {
number += parseInt(temp[0], 10).toString(16);
} else {
number += temp[0];
}
afterdecimal = '.' + temp[1];
}
return number;
}
</script>
</body>
</html>