javascript - 在 n 个字符后拆分输入以与字典对应

标签 javascript dictionary

我正在寻找一种在 n=8 个字符后拆分“二进制到文本”翻译器的输入字符的方法。我假设这是如何使输入与我的二进制字典相对应。 例如: 如果我在输入字段中键入“011000010110001001100011”,输出将显示“abc”(01100001 = a,01100010 = b,01100011 = c)。

但是,当我翻译单个二进制代码时,它似乎工作正常。 例如: 我能够将“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': ' ',
	'00111111': '?',
	'00111010': ':',
	'00101000': '(',
	'00101001': ')',
	'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 {
	font-family: Verdana;
	color: black;
	border: 2px solid #000;
}
</style>
</body></html>

最佳答案

你可以使用 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/>

关于javascript - 在 n 个字符后拆分输入以与字典对应,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40957964/

相关文章:

javascript - 点击错误时展开搜索输入栏

javascript - AngularJS - 级联动态选择列表

javascript - 使用滚动更改静态定位的图像

c# - DataTable 到 Dictionary<String,StringBuilder> 的转换

python - 将列表的 python 列表打印到字符串中

powershell - 将 PowerShell 字典复制到变量中

javascript - Mongoose - 如何从数组元素中删除对象

javascript - 在 Chrome devtools 中测量步骤之间的时间

c# - 集合被修改;枚举操作可能无法执行

python:读取文件并将其拆分为字典列表