@@ -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