-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
193 lines (165 loc) · 8.07 KB
/
index.html
File metadata and controls
193 lines (165 loc) · 8.07 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Slash Bit Code Translator</title>
<script src="https://cdn.tailwindcss.com"></script>
<style>
body {
font-family: 'Inter', sans-serif;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
background-color: #f3f4f6; /* Tailwind gray-100 */
padding: 1rem;
}
.translator-container {
background-color: white;
padding: 2rem;
box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1), 0 4px 6px -2px rgba(0, 0, 0, 0.05); /* Tailwind shadow-lg */
width: 100%;
max-width: 600px;
}
textarea, button {
width: 100%;
padding: 0.75rem; /* Tailwind p-3 */
border: 1px solid #d1d5db; /* Tailwind gray-300 */
font-size: 1rem; /* Tailwind text-base */
box-sizing: border-box;
}
textarea {
min-height: 120px;
resize: vertical;
}
button {
background-color: #3b82f6; /* Tailwind blue-500 */
color: white;
font-weight: 600; /* Tailwind semibold */
cursor: pointer;
transition: background-color 0.2s;
}
button:hover {
background-color: #57CAFF; /* Tailwind blue-600 */
}
button:active {
background-color: #57CAFF; /* Tailwind blue-700 */
}
#outputText {
background-color: #e5e7eb; /* Tailwind gray-200 */
font-family: 'Courier New', Courier, monospace;
}
.message-box {
min-height: 2.5rem;
margin-top: 0.5rem; /* Reduced margin-top slightly as buttons have more mb */
text-align: center;
font-size: 0.875rem; /* Tailwind text-sm */
}
</style>
</head>
<body>
<div class="translator-container rounded-sm">
<h1 class="text-2xl font-bold text-left mb-6 text-gray-700">Slash Bit Code Translator</h1>
<label for="inputText" class="block text-sm font-medium text-gray-700 mb-1">Input Text/Numbers:</label>
<textarea id="inputText" placeholder="Enter text or numbers here (e.g., Hello 123)" class="focus:ring-blue-500 focus:border-blue-500 rounded-sm mb-6"></textarea>
<button id="translateButton" class="rounded-sm mb-6">Translate to Slash Bit Code</button>
<label for="outputText" class="block text-sm font-medium text-gray-700 mb-1">Slash Bit Code Output:</label>
<textarea id="outputText" readonly placeholder="\\\\\\/\ (Example output for '2')" class="focus:ring-blue-500 focus:border-blue-500 rounded-sm mb-4"></textarea>
<button id="copyButton" class="rounded-sm mb-4">Copy Output</button>
<div id="messageBox" class="message-box"></div>
</div>
<script>
// --- Core Translation Logic ---
/**
* Converts a single character to its slash bit code representation.
* @param {string} char - The character to convert.
* @returns {string} The slash bit code representation (e.g., "\\\\\\/\").
*/
function charToSlashBitCode(char) {
let eightBitBinary;
// Rule: If the character is a digit ('0'-'9'),
// its value is represented as "0000" + 4-bit binary of the digit.
if (char >= '0' && char <= '9') {
const digitValue = parseInt(char);
// .toString(2) converts number to binary string.
// .padStart(4, '0') ensures it's 4 bits long, adding leading zeros if needed.
const lowerNibbleBinary = digitValue.toString(2).padStart(4, '0');
eightBitBinary = '0000' + lowerNibbleBinary;
} else {
// Rule: If not a digit, use its 8-bit ASCII value.
const asciiValue = char.charCodeAt(0);
eightBitBinary = asciiValue.toString(2).padStart(8, '0');
// Ensure it's exactly 8 bits, taking the lower 8 bits if charCodeAt resulted in a larger number
// (though for common text, charCodeAt(0) is <= 255).
if (eightBitBinary.length > 8) {
eightBitBinary = eightBitBinary.slice(-8);
}
}
// Split the 8-bit binary into two 4-bit nibbles.
const upperNibbleBinary = eightBitBinary.substring(0, 4);
const lowerNibbleBinary = eightBitBinary.substring(4, 8);
// Convert binary '0's and '1's to '\' and '/' respectively for each nibble.
const slashUpper = upperNibbleBinary.replace(/0/g, '\\').replace(/1/g, '/');
const slashLower = lowerNibbleBinary.replace(/0/g, '\\').replace(/1/g, '/');
// Return the two slash-represented nibbles, joined directly without a space.
return `${slashUpper}${slashLower}`; // MODIFIED: Removed space
}
// --- Event Handler Functions ---
/**
* Handles the translation process when the "Translate" button is clicked.
*/
function handleTranslate() {
const inputText = document.getElementById('inputText').value;
const outputTextarea = document.getElementById('outputText');
if (!inputText) {
outputTextarea.value = ''; // Clear output if input is empty
return;
}
// Split the input string into an array of characters.
const chars = inputText.split('');
// Map each character to its slash bit code representation.
const translatedChars = chars.map(char => charToSlashBitCode(char));
// Join the representations of each character without any space in between.
outputTextarea.value = translatedChars.join(''); // MODIFIED: Changed ' ' to ''
}
/**
* Handles copying the output text to the clipboard.
*/
function handleCopyOutput() {
const outputTextarea = document.getElementById('outputText');
const messageBox = document.getElementById('messageBox');
if (!outputTextarea.value) {
messageBox.textContent = 'Nothing to copy!';
messageBox.className = 'message-box p-2 bg-yellow-100 text-yellow-700 rounded-sm border border-yellow-300';
setTimeout(() => { messageBox.textContent = ''; messageBox.className = 'message-box'; }, 3000);
return;
}
const tempInput = document.createElement('textarea');
tempInput.style.position = 'absolute';
tempInput.style.left = '-9999px';
tempInput.value = outputTextarea.value;
document.body.appendChild(tempInput);
tempInput.select();
tempInput.setSelectionRange(0, 99999);
let success = false;
try {
success = document.execCommand('copy');
} catch (err) {
console.error('Fallback: Oops, unable to copy', err);
messageBox.textContent = 'Failed to copy. Please copy manually.';
messageBox.className = 'message-box p-2 bg-red-100 text-red-700 rounded-sm border border-red-300';
}
document.body.removeChild(tempInput);
if (success) {
messageBox.textContent = 'Copied to clipboard!';
messageBox.className = 'message-box p-2 bg-green-100 text-green-700 rounded-sm border border-green-300';
}
setTimeout(() => { messageBox.textContent = ''; messageBox.className = 'message-box'; }, 3000);
}
// --- Attach Event Listeners ---
document.getElementById('translateButton').addEventListener('click', handleTranslate);
document.getElementById('copyButton').addEventListener('click', handleCopyOutput);
</script>
</body>
</html>