File size: 3,060 Bytes
0bf6fde
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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();
});