2016-12-04 49 views
3

我正在尋找一種方法,在n = 8個字符後,將我的「二進制文本」翻譯器的輸入字符分割。我假設這是如何使輸入符合我的二進制字典。 例如: 如果我在輸入字段中輸入「011000010110001001100011」,輸出會給我「abc」(01100001 = a,01100010 = b,01100011 = c)。在n個字符後分割輸入以與字典對應

但是,當我翻譯單個二進制代碼時它似乎工作正常。例如: 我可以將「01100001」翻譯爲「a」。

這裏是我到目前爲止,修改我已經完成了「文本到二進制」翻譯後:

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

回答

3

你可以使用String#match用正則表達式,查找一組8個字符。

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

這似乎只在工作的時候我要翻譯這句話( 「011000010110001001100011」),除非我做錯了什麼。 – Dennis

+0

它適用於沒有內部空間的任何字符串。 –

+0

謝謝!現在完美運作。 – Dennis