Skip to content

Commit 573c31a

Browse files
committed
Update Puzzle
1 parent d547113 commit 573c31a

File tree

3 files changed

+66
-26
lines changed

3 files changed

+66
-26
lines changed

Coding Puzzle/01_Coding_Puzzle.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
01001101 11100111 10000000 11000101 01100111 01100101 00100000 01100100 01101001 01100101 00100000 01001101 01100001 01100011 01101000 01110100 00100000 01101101 01101001 01110100 00100000 01100100 01101001 01110010 00100000 01110011 01100101 01101001 01101110 00101110
1+
010011011110011110000000110001010110011101100101001000000110010001101001011001010010000001001101011000010110001101101000011101000010000001101101011010010111010000100000011001000110100101110010001000000111001101100101011010010110111000101110
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
0100110111100111100000001100010101100111011001010010000001100100011010010110010100100000 01001101 01100001 01100011 01101000 01110100 00100000 01101101 01101001 01110100 00100000 01100100 01101001 01110010 00100000 01110011 01100101 01101001 01101110 00101110

Coding Puzzle/main.js

Lines changed: 64 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -5,54 +5,93 @@ function handleFileSelect(event) {
55
reader.onload = function() {
66
const binaryString = reader.result.replace(/\s/g, ''); // entferne alle Leerzeichen
77
const asciiString = convertBinaryToAscii(binaryString);
8-
const unicodeString = convertBinaryToUnicode(binaryString);
8+
const unicodeString = convertBinaryToUnicode(binaryString, 'UTF-8'); // Umwandlung in UTF-8
9+
const isoString = convertBinaryToUnicode(binaryString, 'ISO-8859-1'); // Umwandlung in ISO-8859-1
910
// Gib die Ergebnisse aus oder tue etwas anderes damit
1011
console.log('ASCII: ' + asciiString);
1112
console.log('Unicode: ' + unicodeString);
13+
console.log('ISO-8859-1: ' + isoString);
1214
const output = document.getElementById('output');
13-
output.innerHTML = `ASCII: ${asciiString}<br>Unicode: ${unicodeString}`;
15+
output.innerHTML = `ASCII: ${asciiString}<br>Unicode: ${unicodeString}<br>ISO-8859-1: ${isoString}`;
1416
};
1517
}
1618

17-
function convertBinaryToAscii(binaryString) {
18-
let asciiString = '';
19-
// Schleife durch die Binärzeichenkette
20-
for (let i = 0; i < binaryString.length; i += 8) {
21-
// Teile die Binärzeichenkette in 8-Bit-Chunks auf
22-
const chunk = binaryString.substr(i, 8);
23-
// Wandele jeden 8-Bit-Chunk in ASCII-Code um
24-
const charCode = parseInt(chunk, 2);
25-
// Konvertiere ASCII-Code in Zeichen und füge es zur Ausgabezeichenkette hinzu
26-
asciiString += String.fromCharCode(charCode);
27-
}
28-
return asciiString;
29-
}
19+
document.getElementById('fileInput').addEventListener('change', handleFileSelect);
3020

31-
function convertBinaryToUnicode(binaryString) {
21+
function convertBinaryToUnicode(binaryString, charset) {
3222
let unicodeString = '';
23+
let byte1, byte2, byte3, byte4, charCode;
24+
3325
// Schleife durch die Binärzeichenkette
3426
for (let i = 0; i < binaryString.length; i += 8) {
27+
3528
// Wenn das nächste Zeichen ein 16-Bit-Zeichen ist, verarbeite es als solches
3629
if (binaryString.charAt(i) === '1' && binaryString.charAt(i+1) === '0') {
3730
const chunk = binaryString.substr(i, 16);
38-
const charCode = parseInt(chunk, 2);
39-
unicodeString += String.fromCharCode(charCode);
31+
charCode = parseInt(chunk, 2);
4032
i += 8; // überspringe das zweite Byte des 16-Bit-Zeichens
4133
} else {
4234
// sonst verarbeite das Zeichen als 8-Bit-Zeichen
4335
const chunk = binaryString.substr(i, 8);
44-
const charCode = parseInt(chunk, 2);
36+
charCode = parseInt(chunk, 2);
37+
}
38+
39+
// Konvertiere den CharCode in den Unicode-String, abhängig vom Zeichensatz
40+
if (charset === 'UTF-8') {
41+
if (charCode < 128) {
42+
// 1 Byte
43+
unicodeString += String.fromCharCode(charCode);
44+
} else if (charCode < 2048) {
45+
// 2 Bytes
46+
byte1 = 192 + (charCode >> 6);
47+
byte2 = 128 + (charCode & 63);
48+
unicodeString += String.fromCharCode(byte1, byte2);
49+
} else if (charCode < 65536) {
50+
// 3 Bytes
51+
byte1 = 224 + (charCode >> 12);
52+
byte2 = 128 + ((charCode >> 6) & 63);
53+
byte3 = 128 + (charCode & 63);
54+
unicodeString += String.fromCharCode(byte1, byte2, byte3)} else {
55+
// 4 Bytes
56+
byte1 = 240 + (charCode >> 18);
57+
byte2 = 128 + ((charCode >> 12) & 63);
58+
byte3 = 128 + ((charCode >> 6) & 63);
59+
byte4 = 128 + (charCode & 63);
60+
unicodeString += String.fromCharCode(byte1, byte2, byte3, byte4);
61+
}
62+
} else if (charset === 'ISO-8859-1') {
4563
unicodeString += String.fromCharCode(charCode);
64+
} else {
65+
// Standardmäßig wird UTF-8 verwendet
66+
if (charCode < 128) {
67+
unicodeString += String.fromCharCode(charCode);
68+
} else if (charCode < 256) {
69+
byte1 = 194;
70+
byte2 = charCode;
71+
unicodeString += String.fromCharCode(byte1, byte2);
72+
} else {
73+
byte1 = 224 + (charCode >> 12);
74+
byte2 = 128 + ((charCode >> 6) & 63);
75+
byte3 = 128 + (charCode & 63);
76+
unicodeString += String.fromCharCode(byte1, byte2, byte3);
77+
}
4678
}
4779
}
80+
return unicodeString;
81+
}
82+
83+
function convertBinaryToAscii(binaryString) {
84+
let asciiString = '';
4885

49-
// Wenn das erste Zeichen der Unicode-Zeichenkette das BOM ist, entferne es
50-
if (unicodeString.charCodeAt(0) === 65279) {
51-
unicodeString = unicodeString.slice(1);
86+
// Loop through the binary string in chunks of 8
87+
for (let i = 0; i < binaryString.length; i += 8) {
88+
const chunk = binaryString.substr(i, 8);
89+
const charCode = parseInt(chunk, 2); // Convert the binary chunk to decimal
90+
91+
// Convert the decimal charCode to ASCII character
92+
asciiString += String.fromCharCode(charCode);
5293
}
5394

54-
return unicodeString;
95+
return asciiString;
5596
}
5697

57-
const fileInput = document.getElementById('fileInput');
58-
fileInput.addEventListener('change', handleFileSelect);

0 commit comments

Comments
 (0)