2016-12-04 3 views
3

Ich suche nach einer Möglichkeit, die Eingabezeichen meines "binären zu Text" -Übersetzers nach n = 8 Zeichen zu teilen. Ich gehe davon aus, dass so die Eingabe meinem Binärwörterbuch entspricht. Zum Beispiel: Wenn ich "011000010110001001100011" in das Eingabefeld eingib, gibt die Ausgabe mir "abc" (01100001 = a, 01100010 = b, 01100011 = c).Eingabe nach n Zeichen teilen, um dem Wörterbuch zu entsprechen

Allerdings scheint es gut zu funktionieren, wenn ich einzelne Binärcodes übersetze. Zum Beispiel: Ich bin in der Lage, "01100001" in "a" zu übersetzen. Hier

ist, was ich bisher habe, nach meiner schon fertig „Text binäre“ Übersetzer ändern:

<div><center> 
 
<head><title>Binary to text</title></head> 
 
<h3><u>Write letters here:</u><br/></h3> 
 
<input id='inp' /> 
 
<button style="background: white; border: 2px solid #000; font-family: Verdana;" id='butt'>Translate</button> 
 
<br><br>Resultat: <input size="34" type="text" id="out" name="binary" readonly/> 
 
<br> 
 
<br><br><form action="http://localhost/translator/morse-alfabet.php"><input style="width: 400px; background: white; height: 40px; border: 2px solid #000; font-family: Verdana; font-weight:bold;" type="submit" value="Binary to text" /></form></center></div></center></div> 
 
<body background="http://www.pixelstalk.net/wp-content/uploads/2016/10/Free-HD-blurred-wallpaper.jpg"> 
 
<script src='//production-assets.codepen.io/assets/common/stopExecutionOnTimeout-58d22c749295bca52f487966e382a94a495ac103faca9206cbd160bdf8aedf2a.js'></script> 
 
<script>var binary = { 
 
    '01100001': 'a', 
 
    '01100010': 'b', 
 
    '01100011': 'c', 
 
    '01100100': 'd', 
 
    '01100101': 'e', 
 
    '01100110': 'f', 
 
    '01100111': 'g', 
 
    '01101000': 'h', 
 
    '01101001': 'i', 
 
    '01101010': 'j', 
 
    '01101011': 'k', 
 
    '01101100': 'l', 
 
    '01101101': 'm', 
 
    '01101110': 'n', 
 
    '01101111': 'o', 
 
    '01110000': 'p', 
 
    '01110001': 'q', 
 
    '01110010': 'r', 
 
    '01110011': 's', 
 
    '01110100': 't', 
 
    '01110101': 'u', 
 
    '01110110': 'v', 
 
    '01110111': 'w', 
 
    '01111000': 'x', 
 
    '01111001': 'y', 
 
    '01111010': 'z', 
 
    '00110001': '1', 
 
    '00110010': '2', 
 
    '00110011': '3', 
 
    '00110100': '4', 
 
    '00110101': '5', 
 
    '00110110': '6', 
 
    '00110111': '7', 
 
    '00111000': '8', 
 
    '00111001': '9', 
 
    '00110000': '0', 
 
    '00100000': ' ', 
 
\t '00111111': '?', 
 
\t '00111010': ':', 
 
\t '00101000': '(', 
 
\t '00101001': ')', 
 
\t '00101110': '.', 
 
    '00101100': ',', 
 
    '00111011': ';' 
 
}; 
 
var inp = document.getElementById('inp'); 
 
var butt = document.getElementById('butt'); 
 
var out = document.getElementById('out'); 
 
butt.addEventListener('click', function() { 
 
    var conv = inp.value; 
 
    conv = conv.split(' '); 
 
    for (var i = 0; i < conv.length; i++) { 
 
     if (window.CP.shouldStopExecution(1)) { 
 
      break; 
 
     } 
 
     conv[i] = binary[conv[i]]; 
 
    } 
 
    window.CP.exitedLoop(1); 
 
    conv = conv.join(''); 
 
    console.log(conv); 
 
    out.value = conv; 
 
}); 
 
</script> 
 
<style> 
 
div { 
 
    height: 150px; 
 
    width: 400px; 
 
    background: white; 
 

 
    position: fixed; 
 
    top: 50%; 
 
    left: 50%; 
 
    margin-top: -100px; 
 
    margin-left: -200px; 
 
} 
 

 
div { 
 
\t font-family: Verdana; 
 
\t color: black; 
 
\t border: 2px solid #000; 
 
} 
 
</style> 
 
</body></html>

Antwort

3

Sie String#match mit einem regulären Ausdruck verwenden können, die für eine Gruppe sieht von 8 Zeichen.

array = string.match(/.{8}/g) 

var binary = { '01100001': 'a', '01100010': 'b', '01100011': 'c', '01100100': 'd', '01100101': 'e', '01100110': 'f', '01100111': 'g', '01101000': 'h', '01101001': 'i', '01101010': 'j', '01101011': 'k', '01101100': 'l', '01101101': 'm', '01101110': 'n', '01101111': 'o', '01110000': 'p', '01110001': 'q', '01110010': 'r', '01110011': 's', '01110100': 't', '01110101': 'u', '01110110': 'v', '01110111': 'w', '01111000': 'x', '01111001': 'y', '01111010': 'z', '00110001': '1', '00110010': '2', '00110011': '3', '00110100': '4', '00110101': '5', '00110110': '6', '00110111': '7', '00111000': '8', '00111001': '9', '00110000': '0', '00100000': ' ', '00111111': '?', '00111010': ':', '00101000': '(', '00101001': ')', '00101110': '.', '00101100': ',', '00111011': ';' }, 
 
    input = document.getElementById('input'); 
 
    button = document.getElementById('button'); 
 
    out = document.getElementById('out'); 
 

 
button.addEventListener('click', function() { 
 
    var array = input.value.match(/.{8}/g), 
 
     result = (array || []).map(function (a) { // check to prevent iterating non arrays 
 
      return binary[a] || 'unknown'; 
 
     }).join(''); 
 

 
    out.value = result; 
 
});
Write letters here: <input id="input" /> 
 
<button id="button">Translate</button><br><br> 
 
Result: <input size="34" type="text" id="out" name="binary" readonly/>

+0

Das nur scheint zu funktionieren, wenn ich diesen Satz übersetzen ("011000010110001001100011"), es sei denn ich etwas falsch tue. – Dennis

+0

funktioniert es für alle Zeichenfolgen ohne Leerzeichen. –

+0

Danke! Funktioniert jetzt perfekt. – Dennis

Verwandte Themen