admin管理员组文章数量:1392007
I want to make a binary to text converter. I have already made a text to binary converter. Here is the code for that:
function convertText() {
var output = document.getElementById("binaryConvert");
var input = document.getElementById("textConvert").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;
}
}
But I don't know how to convert binary to text. Anyone know? Also if it is not binary they are inputting then in the textConvert box it has a value of "That is not binary you are inputting" or something like that. Here is all of my code:
<!doctype html>
<html>
<head>
<title>Binary Converter</title>
<script type="text/javascript" src=".10.2/jquery.min.js"></script>
<script type="text/javascript">
function convertText() {
var output = document.getElementById("binaryConvert");
var input = document.getElementById("textConvert").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>
<noscript>
Please enable JavaScript in your browsers settings to use this tool.
</noscript>
</head>
<body>
<center>
<div class="container">
<span class="main">Binary Converter</span><br>
<textarea class="textConvert" id="textConvert" onKeyUp="convertText()" placeholder="Input text to convert to Binary"></textarea>
<textarea class="binaryConvert" id="binaryConvert" placeholder="Binary text" readonly></textarea>
<div class="about">Made by <strong>Omar Latreche</strong></div>
</div>
</center>
</body>
</html>
Edit: I figured it out. Here is the code:
function convertBinary() {
var bintext, textresult="", binlength;
bintext = document.getElementById("text_result").value.replace(/[^01]/g, "");
binlength = bintext.length-(bintext.length%8);
for (z=0; z < binlength; z=z+8) {
textresult += String.fromCharCode(parseInt(bintext.substr(z,8),2));
}
document.getElementById("text_convert").value = textresult;
}
Thanks for all answers!
I want to make a binary to text converter. I have already made a text to binary converter. Here is the code for that:
function convertText() {
var output = document.getElementById("binaryConvert");
var input = document.getElementById("textConvert").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;
}
}
But I don't know how to convert binary to text. Anyone know? Also if it is not binary they are inputting then in the textConvert box it has a value of "That is not binary you are inputting" or something like that. Here is all of my code:
<!doctype html>
<html>
<head>
<title>Binary Converter</title>
<script type="text/javascript" src="http://ajax.googleapis./ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript">
function convertText() {
var output = document.getElementById("binaryConvert");
var input = document.getElementById("textConvert").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>
<noscript>
Please enable JavaScript in your browsers settings to use this tool.
</noscript>
</head>
<body>
<center>
<div class="container">
<span class="main">Binary Converter</span><br>
<textarea class="textConvert" id="textConvert" onKeyUp="convertText()" placeholder="Input text to convert to Binary"></textarea>
<textarea class="binaryConvert" id="binaryConvert" placeholder="Binary text" readonly></textarea>
<div class="about">Made by <strong>Omar Latreche</strong></div>
</div>
</center>
</body>
</html>
Edit: I figured it out. Here is the code:
function convertBinary() {
var bintext, textresult="", binlength;
bintext = document.getElementById("text_result").value.replace(/[^01]/g, "");
binlength = bintext.length-(bintext.length%8);
for (z=0; z < binlength; z=z+8) {
textresult += String.fromCharCode(parseInt(bintext.substr(z,8),2));
}
document.getElementById("text_convert").value = textresult;
}
Thanks for all answers!
Share Improve this question edited Jun 6, 2017 at 20:41 Alfabravo 7,5896 gold badges49 silver badges83 bronze badges asked Jan 26, 2014 at 11:02 16.uk16.uk 5793 gold badges9 silver badges19 bronze badges 1- It'd be great to set the solution as an answer to mark it as accepted, so others will now that the question is solved :) – Alfabravo Commented Jun 6, 2017 at 20:41
3 Answers
Reset to default 3Yes, use parseInt as suggested above - and consider using (+input).toString(2) to convert your input to binary :)
function convertText() {
var output = document.getElementById("binaryConvert");
var input = document.getElementById("textConvert").value;
output.value = (+input).toString(2);
}
function convertBinary() {
var output = document.getElementById("textConvert");
var input = document.getElementById("binaryConvert").value;
output.value = parseInt(input,2);
}
You can use parseInt()
with a radix of 2.
Ex: parseInt('10001', 2);
returns 17
And if your input string isn't valid binary, it returns NaN
which you can test using isNaN()
function binarytoString(str) {
return str.split(/\s/).map(function (val){
return String.fromCharCode(parseInt(val, 2));
}).join("");
}
本文标签: htmlConvert Binary to text using JavaScriptStack Overflow
版权声明:本文标题:html - Convert Binary to text using JavaScript - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744763890a2623917.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论