使用javascript将二进制文件转换为文本 [英] Converting Binary to text using javascript
问题描述
如何将二进制代码转换为使用javascript的文本?我已经将文本转换为二进制文件,但有没有办法以相反的方式进行操作?这是我的代码:
<!doctype html>
< html>
< head>
< title> Binary Converter< / title>
< script type =text / javascriptsrc =http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js>< / script>
< script type =text / javascript>
函数convertBinary(){
var output = document.getElementById(outputBinary);
var input = document.getElementById(inputBinary).value;
output.value =;
for(i = 0; i< input.length; i ++){var e = input [i] .charCodeAt(0); var s =;
do {
var a = e%2;
e =(e-a)/ 2;
s = a + s;
} while(e!= 0); (s.length< 8){s =0+ s;}
output.value + = s;
}
}
< / script>
< / head>
< body>
< center>
< div class =container>
< span class =main>二进制转换器< / span>< br>
< textarea class =outputBinaryid =outputBinaryreadonly>< / textarea>
< div class =about>制作者< strong> Omar< / strong>< / div>
< / div>
< / center>
< / body>
< / html>
任何帮助将不胜感激。谢谢,奥马尔。
使用 toString(2)
二进制字符串。例如:
var input = document.getElementById(inputDecimal).value;
document.getElementById(outputBinary)。value = parseInt(input).toString(2);
或 parseInt(input,10)
if你知道输入应该是十进制的。否则输入0x42将被解析为十六进制而不是十进制。
编辑:重新阅读问题。从二进制到文本,使用parseInt(input,2).toString(10)。
以上所有内容仅适用于数字。例如, 4
< - > 0100
。如果你想要 4
< - > decimal 52(它的ASCII值),可以使用 String.fromCharCode()
(参见这个答案)。
编辑2:每次请求(
函数BinToText(){
var input = document.getElementById(inputBinary )。值;
document.getElementById(outputText)。value = parseInt(input,2).toString(10);
}
...
< textarea class =outputBinaryid =outputTextreadonly>< / textarea>
如果您将 0100
放入 inputBinary
,您应该在 outputText
(未测试)中获得 4
。
How can I convert Binary code to text using javascript? I have already made it convert text to binary but is there a way of doing it the other way around? Here is my code:
<!doctype html>
<html>
<head>
<title>Binary Converter</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function convertBinary() {
var output = document.getElementById("outputBinary");
var input = document.getElementById("inputBinary").value;
output.value = "";
for (i=0; i < input.length; i++) {var e=input[i].charCodeAt(0);var s = "";
do{
var a =e%2;
e=(e-a)/2;
s=a+s;
}while(e!=0);
while(s.length<8){s="0"+s;}
output.value+=s;
}
}
</script>
</head>
<body>
<center>
<div class="container">
<span class="main">Binary Converter</span><br>
<textarea autofocus class="inputBinary" id="inputBinary" onKeyUp="convertBinary()"></textarea>
<textarea class="outputBinary" id="outputBinary" readonly></textarea>
<div class="about">Made by <strong>Omar</strong></div>
</div>
</center>
</body>
</html>
Any help will be greatly appreciated. Thanks, Omar.
Use toString(2)
to convert to a binary string. For example:
var input = document.getElementById("inputDecimal").value;
document.getElementById("outputBinary").value = parseInt(input).toString(2);
or parseInt(input,10)
if you know the input should be decimal. Otherwise input of "0x42" will be parsed as hex rather than decimal.
EDIT: Just re-read the question. To go from binary to text, use parseInt(input,2).toString(10).
Everything above is for numbers only. E.g., 4
<-> 0100
. If you want 4
<-> decimal 52 (its ASCII value), use String.fromCharCode()
(see this answer).
EDIT 2: per request for where everything fits, try this:
function BinToText() {
var input = document.getElementById("inputBinary").value;
document.getElementById("outputText").value = parseInt(input,2).toString(10);
}
...
<textarea autofocus class="inputBinary" id="inputBinary" onKeyUp="BinToText()"></textarea>
<textarea class="outputBinary" id="outputText" readonly></textarea>
If you put 0100
in inputBinary
, you should get 4
in outputText
(not tested).
这篇关于使用javascript将二进制文件转换为文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!