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.com/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!