23code / script.js
amirinvis's picture
create an app that turns messages into binary code but not 0 and 1. 2 and 3 numbers and has a decode and encode place
0bf6fde verified
Raw
History Blame Contribute Delete
3.06 kB
document.addEventListener('DOMContentLoaded', function() {
// Encode function
document.getElementById('encode-btn').addEventListener('click', function() {
const input = document.getElementById('encode-input').value;
if (!input) {
alert('Please enter a message to encode');
return;
}
let binaryString = '';
for (let i = 0; i < input.length; i++) {
// Get ASCII code of each character
const charCode = input.charCodeAt(i);
// Convert to 8-bit binary
let binary = charCode.toString(2).padStart(8, '0');
// Replace 0 with 2 and 1 with 3
binary = binary.replace(/0/g, '2').replace(/1/g, '3');
binaryString += binary + ' ';
}
document.getElementById('encode-output').value = binaryString.trim();
});
// Decode function
document.getElementById('decode-btn').addEventListener('click', function() {
const input = document.getElementById('decode-input').value.trim();
if (!input) {
alert('Please enter a message to decode');
return;
}
// Split into 8-character chunks (assuming 8-bit encoding)
const chunks = input.split(' ');
let output = '';
for (const chunk of chunks) {
if (chunk.length !== 8) {
alert('Invalid encoded message. Each character should be represented by 8 digits (2s and 3s).');
return;
}
// Replace 2 with 0 and 3 with 1
const binary = chunk.replace(/2/g, '0').replace(/3/g, '1');
// Convert binary to ASCII code
const charCode = parseInt(binary, 2);
// Convert ASCII code to character
output += String.fromCharCode(charCode);
}
document.getElementById('decode-output').value = output;
});
// Copy to clipboard functions
document.getElementById('copy-encode').addEventListener('click', function() {
const output = document.getElementById('encode-output');
output.select();
document.execCommand('copy');
// Show feedback
const originalText = this.innerHTML;
this.innerHTML = '<i data-feather="check" class="w-5 h-5"></i> Copied!';
setTimeout(() => {
this.innerHTML = originalText;
feather.replace();
}, 2000);
});
document.getElementById('copy-decode').addEventListener('click', function() {
const output = document.getElementById('decode-output');
output.select();
document.execCommand('copy');
// Show feedback
const originalText = this.innerHTML;
this.innerHTML = '<i data-feather="check" class="w-5 h-5"></i> Copied!';
setTimeout(() => {
this.innerHTML = originalText;
feather.replace();
}, 2000);
});
// Initialize feather icons
feather.replace();
});