-
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathbinaryconvertor.html
More file actions
346 lines (308 loc) · 11.3 KB
/
binaryconvertor.html
File metadata and controls
346 lines (308 loc) · 11.3 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
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
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
<!DOCTYPE html>
<html>
<head>
<title>8-bit Converter</title>
<style>
body {
font-family: Arial, sans-serif;
margin: 20px;
}
.result {
margin-top: 10px;
}
.workings {
margin-top: 20px;
font-family: monospace;
}
.slider-container {
margin-bottom: 20px;
}
#binaryInput {
background-color: #99ccff;
}
#hexadecimalInput {
background-color: #99ff99;
}
table {
width: 100%;
border-collapse: collapse;
margin-top: 20px;
}
table, th, td {
border: 1px solid black;
}
th, td {
text-align: center;
padding: 8px;
}
.step2-table {
margin-top: 10px;
border: 1px solid black;
border-collapse: collapse;
width: 100%;
}
.step2-table td, .step2-table th {
border: 1px solid black;
padding: 5px;
text-align: center;
}
</style>
</head>
<body>
<h2>8-bit Converter</h2>
<div>
<label for="mode">Mode:</label>
<input type="radio" id="unsigned" name="mode" value="unsigned" checked onchange="toggleTwosComplement()"> Unsigned
<input type="radio" id="twosComplement" name="mode" value="twosComplement" onchange="toggleTwosComplement()"> Two's Complement
</div>
<br>
<div class="slider-container">
<label for="decimalInput">Decimal (Slider):</label>
<input type="range" id="decimalInput" min="0" max="255" value="0" oninput="convertFromDecimal()">
<span id="decimalValue">0</span>
</div>
<div class="slider-container">
<label for="decimalTextInput">Decimal (Input):</label>
<input type="number" id="decimalTextInput" min="-128" max="255" value="0" oninput="convertFromTextInput()">
</div>
<div class="slider-container">
<label for="binaryInput">Binary:</label>
<input type="range" id="binaryInput" min="0" max="255" value="0" oninput="convertFromBinary()">
<span id="binaryValue">0000 0000</span>
</div>
<div class="slider-container">
<label for="hexadecimalInput">Hexadecimal:</label>
<input type="range" id="hexadecimalInput" min="0" max="255" value="0" oninput="convertFromHexadecimal()">
<span id="hexadecimalValue">00</span>
</div>
<div class="result" id="conversionResult"></div>
<div class="workings" id="conversionWorkings"></div>
<table>
<tr>
<th>Bit</th>
<td id="bit0">0</td>
<td id="bit1">0</td>
<td id="bit2">0</td>
<td id="bit3">0</td>
<td id="bit4">0</td>
<td id="bit5">0</td>
<td id="bit6">0</td>
<td id="bit7">0</td>
</tr>
<tr>
<th>Place Value</th>
<td>2<sup>7</sup></td>
<td>2<sup>6</sup></td>
<td>2<sup>5</sup></td>
<td>2<sup>4</sup></td>
<td>2<sup>3</sup></td>
<td>2<sup>2</sup></td>
<td>2<sup>1</sup></td>
<td>2<sup>0</sup></td>
</tr>
<tr>
<th>Value</th>
<td>128</td>
<td>64</td>
<td>32</td>
<td>16</td>
<td>8</td>
<td>4</td>
<td>2</td>
<td>1</td>
</tr>
<tr>
<th>Hexadecimal</th>
<td colspan="4" id="hex0">0</td>
<td colspan="4" id="hex1">0</td>
</tr>
</table>
<p>With thanks to Su Young for the binary workings algorithm and Adit for the idea to include a text input.</p>
<script>
function toggleTwosComplement() {
const twosComplement = document.getElementById("twosComplement").checked;
const decimalInput = document.getElementById("decimalInput");
const decimalTextInput = document.getElementById("decimalTextInput");
if (twosComplement) {
decimalInput.min = -128;
decimalInput.max = 127;
decimalTextInput.min = -128;
decimalTextInput.max = 127;
} else {
decimalInput.min = 0;
decimalInput.max = 255;
decimalTextInput.min = 0;
decimalTextInput.max = 255;
}
// Update the conversion based on the current slider or text input value
convertFromDecimal();
}
function clearWorkings() {
document.getElementById("conversionWorkings").innerHTML = "";
}
function convertFromDecimal() {
clearWorkings();
let decimal = parseInt(document.getElementById("decimalInput").value);
document.getElementById("decimalTextInput").value = decimal;
handleConversion(decimal);
}
function convertFromTextInput() {
clearWorkings();
let decimal = parseInt(document.getElementById("decimalTextInput").value);
document.getElementById("decimalInput").value = decimal;
handleConversion(decimal);
}
function handleConversion(decimal) {
const twosComplement = document.getElementById("twosComplement").checked;
document.getElementById("decimalValue").innerText = decimal;
if (isNaN(decimal) || decimal < (twosComplement ? -128 : 0) || decimal > (twosComplement ? 127 : 255)) {
document.getElementById("conversionResult").innerText = `Please enter a valid decimal number between ${twosComplement ? -128 : 0} and ${twosComplement ? 127 : 255}.`;
document.getElementById("conversionWorkings").innerText = "";
return;
}
let binary;
if (twosComplement && decimal < 0) {
binary = (256 + decimal).toString(2).padStart(8, '0');
} else {
binary = decimal.toString(2).padStart(8, '0');
}
let hexadecimal = parseInt(binary, 2).toString(16).toUpperCase().padStart(2, '0');
document.getElementById("binaryInput").value = parseInt(binary, 2);
document.getElementById("binaryValue").innerText = binary.slice(0, 4) + " " + binary.slice(4);
document.getElementById("hexadecimalInput").value = parseInt(binary, 2);
document.getElementById("hexadecimalValue").innerText = hexadecimal;
displayConversions(decimal, binary, hexadecimal);
showDetailedWorkings(decimal);
updateBinaryTable(binary);
updateHexadecimalTable(hexadecimal);
}
function convertFromBinary() {
clearWorkings();
let binaryValue = parseInt(document.getElementById("binaryInput").value);
const binary = binaryValue.toString(2).padStart(8, '0');
const twosComplement = document.getElementById("twosComplement").checked;
document.getElementById("binaryValue").innerText = binary.slice(0, 4) + " " + binary.slice(4);
if (isNaN(binaryValue) || binaryValue < 0 || binaryValue > 255) {
document.getElementById("conversionResult").innerText = "Please enter a valid 8-bit binary number.";
document.getElementById("conversionWorkings").innerText = "";
return;
}
let decimal = parseInt(binary, 2);
if (twosComplement && binary[0] === '1') {
decimal = decimal - 256;
}
let hexadecimal = parseInt(binary, 2).toString(16).toUpperCase().padStart(2, '0');
document.getElementById("decimalInput").value = decimal;
document.getElementById("decimalTextInput").value = decimal;
document.getElementById("decimalValue").innerText = decimal;
document.getElementById("hexadecimalInput").value = parseInt(binary, 2);
document.getElementById("hexadecimalValue").innerText = hexadecimal;
displayConversions(decimal, binary, hexadecimal);
updateBinaryTable(binary);
updateHexadecimalTable(hexadecimal);
}
function convertFromHexadecimal() {
clearWorkings();
let hexadecimalValue = parseInt(document.getElementById("hexadecimalInput").value);
const hexadecimal = hexadecimalValue.toString(16).toUpperCase().padStart(2, '0');
const twosComplement = document.getElementById("twosComplement").checked;
document.getElementById("hexadecimalValue").innerText = hexadecimal;
if (isNaN(hexadecimalValue) || hexadecimalValue < 0 || hexadecimalValue > 255) {
document.getElementById("conversionResult").innerText = "Please enter a valid 2-digit hexadecimal number.";
document.getElementById("conversionWorkings").innerText = "";
return;
}
let decimal = parseInt(hexadecimal, 16);
const binary = decimal.toString(2).padStart(8, '0');
if (twosComplement && binary[0] === '1') {
decimal = decimal - 256;
}
document.getElementById("decimalInput").value = decimal;
document.getElementById("decimalTextInput").value = decimal;
document.getElementById("decimalValue").innerText = decimal;
document.getElementById("binaryInput").value = parseInt(binary, 2);
document.getElementById("binaryValue").innerText = binary.slice(0, 4) + " " + binary.slice(4);
displayConversions(decimal, binary, hexadecimal);
updateBinaryTable(binary);
updateHexadecimalTable(hexadecimal);
}
function displayConversions(decimal, binary, hexadecimal) {
document.getElementById("conversionResult").innerHTML = `
<strong>Conversions:</strong><br>
Decimal: ${decimal}<br>
Binary: ${binary.slice(0, 4)} ${binary.slice(4)}<br>
Hexadecimal: ${hexadecimal}<br>
`;
}
function showDetailedWorkings(decimal) {
const twosComplement = document.getElementById("twosComplement").checked;
let workings = "";
const line = Array(8).fill(' ');
// Only show steps 1 and 2 for unsigned or non-negative two's complement numbers
if (!twosComplement || (twosComplement && decimal >= 0)) {
workings += "Step 1: Continuously divide the number by 2, noting down the quotient and remainder each time, until the quotient reaches 0.<br><br>";
// Step 1
let n = decimal;
let depth = 0;
let step1Results = [];
while (n > 0) {
step1Results.push(`${n}/2 = ${Math.floor(n / 2)} remainder ${n % 2}<br>`);
n = Math.floor(n / 2);
depth++;
}
step1Results.forEach(result => workings += result);
// Step 2
n = decimal;
depth = 7;
workings += "<br><br>Step 2: Write the numbers in order from right to left.<br><br>";
workings += `<table class="step2-table"><tr><th>Division</th><th colspan="8">Binary Representation</th></tr>`;
while (n > 0) {
line[depth] = (n % 2).toString();
workings += `<tr><td>${n}/2</td>`;
for (let i = 0; i < 8; i++) {
workings += `<td>${line[i]}</td>`;
}
workings += `</tr>`;
n = Math.floor(n / 2);
depth--;
}
workings += `</table>`;
}
// Step 3 (Show for unsigned binary and positive two's complement numbers)
if (!twosComplement || (twosComplement && decimal > 0)) {
if (line.includes(' ')) {
workings += "<br>Step 3: Add the 0s and check you have 8 numbers<br><br>";
for (let i = 0; i < 8; i++) {
if (line[i] === ' ') {
line[i] = '0';
}
}
workings += `<table class="step2-table"><tr>`;
for (let i = 0; i < 8; i++) {
workings += `<td>${line[i]}</td>`;
}
workings += `</tr></table>`;
}
}
// Final Answer
const binaryResult = line.join(' ');
// Display the results
document.getElementById("conversionWorkings").innerHTML = workings;
document.getElementById("binaryValue").innerText = binaryResult;
}
function updateBinaryTable(binary) {
for (let i = 0; i < 8; i++) {
document.getElementById(`bit${i}`).innerText = binary[i];
}
}
function updateHexadecimalTable(hexadecimal) {
document.getElementById('hex0').innerText = hexadecimal[0];
document.getElementById('hex1').innerText = hexadecimal[1];
}
// Initial call to display the default value
convertFromDecimal();
</script>
<script async defer src="https://scripts.simpleanalyticscdn.com/latest.js"></script>
<noscript><img src="https://queue.simpleanalyticscdn.com/noscript.gif" alt="" referrerpolicy="no-referrer-when-downgrade" /></noscript>
</body>
</html>