-
Notifications
You must be signed in to change notification settings - Fork 54
Expand file tree
/
Copy pathtext-binary-converter.html
More file actions
455 lines (405 loc) · 14.9 KB
/
Copy pathtext-binary-converter.html
File metadata and controls
455 lines (405 loc) · 14.9 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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Text to Binary Converter — One File Tools</title>
<style>
/* ========================================
Reset & Theme Variable Base Rules
======================================== */
*,
*::before,
*::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
:root {
--bg-main: #f8fafc;
--bg-panel: #ffffff;
--text-main: #0f172a;
--text-muted: #64748b;
--border-color: #e2e8f0;
--accent-color: #10b981;
--accent-hover: #059669;
--accent-secondary: #3b82f6;
--code-bg: #f1f5f9;
}
@media (prefers-color-scheme: dark) {
:root {
--bg-main: #0f172a;
--bg-panel: #1e293b;
--text-main: #f8fafc;
--text-muted: #94a3b8;
--border-color: #334155;
--accent-color: #34d399;
--accent-hover: #10b981;
--accent-secondary: #60a5fa;
--code-bg: #0f172a;
}
}
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background-color: var(--bg-main);
color: var(--text-main);
line-height: 1.6;
padding: 2rem;
max-width: 1400px;
margin: 0 auto;
transition:
background 0.3s,
color 0.3s;
}
header {
margin-bottom: 2rem;
border-bottom: 1px solid var(--border-color);
padding-bottom: 1.5rem;
}
header h1 {
font-size: 2.2rem;
font-weight: 800;
letter-spacing: -0.5px;
margin-bottom: 0.5rem;
}
header p {
color: var(--text-muted);
font-size: 1.05rem;
}
/* ========================================
Toolbar & Modes Architecture
======================================== */
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
flex-wrap: wrap;
gap: 1rem;
background-color: var(--bg-panel);
padding: 1rem;
border-radius: 8px;
border: 1px solid var(--border-color);
margin-bottom: 1.5rem;
}
.mode-group {
display: flex;
background-color: var(--bg-main);
padding: 0.25rem;
border-radius: 8px;
border: 1px solid var(--border-color);
}
.mode-btn {
padding: 0.5rem 1.25rem;
border-radius: 6px;
border: none;
background: transparent;
color: var(--text-muted);
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
.mode-btn.active {
background-color: var(--bg-panel);
color: var(--accent-secondary);
box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}
button.btn-action {
padding: 0.6rem 1rem;
border-radius: 6px;
border: 1px solid var(--border-color);
background-color: var(--bg-main);
color: var(--text-main);
font-size: 0.9rem;
font-weight: 600;
cursor: pointer;
transition: all 0.2s;
}
button.btn-action:hover {
background-color: var(--code-bg);
}
/* ========================================
Workspace Grid System
======================================== */
.workspace-grid {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
margin-bottom: 1.5rem;
}
@media (min-width: 900px) {
.workspace-grid {
grid-template-columns: 1fr 1fr;
}
}
.editor-container {
display: flex;
flex-direction: column;
background-color: var(--bg-panel);
border: 1px solid var(--border-color);
border-radius: 10px;
overflow: hidden;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.05);
}
.editor-header {
display: flex;
justify-content: space-between;
align-items: center;
background-color: var(--bg-main);
padding: 0.75rem 1rem;
border-bottom: 1px solid var(--border-color);
font-weight: 700;
font-size: 0.9rem;
text-transform: uppercase;
letter-spacing: 0.5px;
color: var(--accent-secondary);
}
textarea {
width: 100%;
height: 380px;
padding: 1rem;
border: none;
background-color: var(--bg-panel);
color: var(--text-main);
font-family: "SFMono-Regular", Consolas, "Liberation Mono", Menlo, Courier, monospace;
font-size: 0.95rem;
resize: vertical;
outline: none;
line-height: 1.6;
}
/* ========================================
Visual Bit Matrix Table Lookups
======================================== */
.analysis-panel {
background-color: var(--bg-panel);
border: 1px solid var(--border-color);
border-radius: 10px;
padding: 1.5rem;
}
.analysis-panel h3 {
font-size: 1rem;
margin-bottom: 1rem;
color: var(--text-main);
display: flex;
justify-content: space-between;
}
.bit-grid {
display: flex;
gap: 0.5rem;
flex-wrap: wrap;
margin-top: 0.5rem;
}
.bit-block {
background-color: var(--code-bg);
border: 1px solid var(--border-color);
padding: 0.5rem 0.75rem;
border-radius: 6px;
text-align: center;
font-family: monospace;
min-width: 60px;
}
.bit-block .char {
font-weight: 800;
color: var(--accent-color);
display: block;
font-size: 1rem;
margin-bottom: 0.25rem;
}
.bit-block .binary {
font-size: 0.8rem;
color: var(--text-muted);
}
footer {
margin-top: 3rem;
text-align: center;
font-size: 0.9rem;
border-top: 1px solid var(--border-color);
padding-top: 1.5rem;
color: var(--text-muted);
}
footer a {
color: var(--accent-secondary);
text-decoration: none;
font-weight: 500;
}
footer a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<header>
<h1>ASCII / UTF-8 Text to Binary Converter</h1>
<p>Encode plain text characters into binary byte structures, or translate stream bits back into readable text.</p>
</header>
<main>
<div class="toolbar">
<div class="mode-group">
<button id="modeEncode" class="mode-btn active">🔒 Text to Binary</button>
<button id="modeDecode" class="mode-btn">🔓 Binary to Text</button>
</div>
<div style="display: flex; gap: 0.5rem">
<button id="sampleBtn" class="btn-action">📄 Load Sample</button>
<button id="clearBtn" class="btn-action">🗑 Clear</button>
</div>
</div>
<div class="workspace-grid">
<div class="editor-container">
<div id="labelInput" class="editor-header">📦 Input Source Plain Text</div>
<textarea id="sourceInput" placeholder="Type or paste your data text characters here..."></textarea>
</div>
<div class="editor-container">
<div class="editor-header">
<span id="labelOutput">📋 Output Target Binary bits</span>
<button id="copyBtn" style="padding: 0.25rem 0.6rem; font-size: 0.75rem; border-radius: 4px; border: 1px solid var(--border-color); background: var(--bg-main); color: var(--text-main); cursor: pointer">📋 Copy</button>
</div>
<textarea id="targetOutput" placeholder="The resulting values will process automatically..." readonly></textarea>
</div>
</div>
<div class="analysis-panel">
<h3 id="analysisHeader">🔍 Dynamic Byte Visualization Map <span id="charCount" style="font-size: 0.85rem; color: var(--text-muted)">0 Characters parsed</span></h3>
<div id="bitGrid" class="bit-grid">
<div style="color: var(--text-muted); font-size: 0.9rem; font-style: italic">Enter text data layout matrix to inspect individual byte blocks...</div>
</div>
</div>
</main>
<footer>
<p>Part of <a href="https://github.com/praveenscience/One-File-Tools">One File Tools</a></p>
</footer>
<script>
// DOM Selectors Map
const sourceInput = document.getElementById("sourceInput");
const targetOutput = document.getElementById("targetOutput");
const modeEncode = document.getElementById("modeEncode");
const modeDecode = document.getElementById("modeDecode");
const labelInput = document.getElementById("labelInput");
const labelOutput = document.getElementById("labelOutput");
const sampleBtn = document.getElementById("sampleBtn");
const clearBtn = document.getElementById("clearBtn");
const copyBtn = document.getElementById("copyBtn");
const bitGrid = document.getElementById("bitGrid");
const charCount = document.getElementById("charCount");
let currentMode = "encode"; // 'encode' | 'decode'
// Core Text-To-Binary Conversion Logic
function convertTextToBinary(text) {
let binaryOutput = [];
let visualMapHtml = "";
for (let i = 0; i < text.length; i++) {
const charCode = text.charCodeAt(i);
// Convert to binary string representation and pad to full 8-bit octet length
const binaryByte = charCode.toString(2).padStart(8, "0");
binaryOutput.push(binaryByte);
// Escape space characters visuals and HTML-escape for safe display
const printChar = text[i] === " " ? "␣" : text[i] === "\n" ? "↵" : text[i].replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
visualMapHtml += `
<div class="bit-block">
<span class="char">${printChar}</span>
<span class="binary">${binaryByte}</span>
</div>
`;
}
targetOutput.value = binaryOutput.join(" ");
bitGrid.innerHTML = visualMapHtml || '<div style="color: var(--text-muted); font-size: 0.9rem; font-style: italic;">Enter text data layouts to inspect bits...</div>';
charCount.textContent = `${text.length} Character${text.length === 1 ? "" : "s"} mapped`;
}
// Core Binary-To-Text Decoding Logic
function convertBinaryToText(binaryStr) {
// Standardize string entries by stripping extraneous symbols
const sanitized = binaryStr.replace(/[^01\s]/g, "").trim();
if (!sanitized) {
targetOutput.value = "";
bitGrid.innerHTML = "";
charCount.textContent = "0 Characters parsed";
return;
}
// Split into array segments by space boundaries
const blocks = sanitized.split(/\s+/);
let textOutput = "";
let visualMapHtml = "";
blocks.forEach((block) => {
if (block.length > 0) {
const charCode = parseInt(block, 2);
if (!isNaN(charCode)) {
const char = String.fromCharCode(charCode);
textOutput += char;
const printChar = char === " " ? "␣" : char === "\n" ? "↵" : char.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """);
visualMapHtml += `
<div class="bit-block">
<span class="char">${printChar}</span>
<span class="binary">${block.padStart(8, "0")}</span>
</div>
`;
}
}
});
targetOutput.value = textOutput;
bitGrid.innerHTML = visualMapHtml;
charCount.textContent = `${textOutput.length} Character${textOutput.length === 1 ? "" : "s"} extracted`;
}
// Orchestration Router Trigger
function handleRealtimeConversion() {
const val = sourceInput.value;
if (currentMode === "encode") {
convertTextToBinary(val);
} else {
convertBinaryToText(val);
}
}
// Mode Switching UI Handlers
function switchMode(mode) {
currentMode = mode;
sourceInput.value = "";
targetOutput.value = "";
bitGrid.innerHTML = '<div style="color: var(--text-muted); font-size: 0.9rem; font-style: italic;">Enter contents to analyze metrics...</div>';
charCount.textContent = "0 Characters parsed";
if (mode === "encode") {
modeEncode.classList.add("active");
modeDecode.classList.remove("active");
labelInput.textContent = "📦 Input Source Plain Text";
labelOutput.textContent = "📋 Output Target Binary Bits";
sourceInput.placeholder = "Type or paste your text characters here...";
} else {
modeDecode.classList.add("active");
modeEncode.classList.remove("active");
labelInput.textContent = "📦 Input Source Binary Bits Stream";
labelOutput.textContent = "📋 Output Target Plain Text Decoded";
sourceInput.placeholder = "Enter strings of 8-bit characters (e.g., 01001000 01001001)...";
}
}
// Clipboards Engine Copy Actions
function copyOutput() {
const outTxt = targetOutput.value;
if (!outTxt) return;
navigator.clipboard.writeText(outTxt).then(() => {
const label = copyBtn.textContent;
copyBtn.textContent = "✔ Copied!";
setTimeout(() => {
copyBtn.textContent = label;
}, 1500);
});
}
// Event Listeners Registration
sourceInput.addEventListener("input", handleRealtimeConversion);
modeEncode.addEventListener("click", () => switchMode("encode"));
modeDecode.addEventListener("click", () => switchMode("decode"));
copyBtn.addEventListener("click", copyOutput);
clearBtn.addEventListener("click", () => {
sourceInput.value = "";
targetOutput.value = "";
bitGrid.innerHTML = '<div style="color: var(--text-muted); font-size: 0.9rem; font-style: italic;">Workspace cleared.</div>';
charCount.textContent = "0 Characters parsed";
});
sampleBtn.addEventListener("click", () => {
if (currentMode === "encode") {
sourceInput.value = "Hello World!";
} else {
sourceInput.value = "01001000 01000101 01001100 01001100 01001111 00100000 01010111 01001111 01010010 01001100 01000100 00100001";
}
handleRealtimeConversion();
});
// Initialize Startup Context Settings Defaults
sourceInput.value = "Hello World!";
handleRealtimeConversion();
</script>
</body>
</html>