2

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!

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 :) Commented Jun 6, 2017 at 20:41

3 Answers 3

3

Yes, 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);
}
Sign up to request clarification or add additional context in comments.

4 Comments

Could you please make a JSFiddle of this? I'm a bit confused.
It does for me. Had a typo in the convertBinary, but parseInt("111",2) yields 7 and (+7).totring(2) yields "111"...
Could you make a jsfiddle of it please? I don't know where everything goes. I'm confused.
You have made it work in one direction (decimal->binary). I trust you to be able to make the binary->decimal work. :)
1

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

1 Comment

Could you show me where all of this would go please? Like put it into my code? I'm a beginner.
1
function binarytoString(str) {
  return str.split(/\s/).map(function (val){
    return String.fromCharCode(parseInt(val, 2));
  }).join("");
}

1 Comment

While this code may answer the question, providing additional context regarding how and/or why it solves the problem would improve the answer's long-term value.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.