0

I want to make a text to binary converter. For example if someone types in 'Hello' it would return 0100100001100101011011000110110001101111 How can I do this? I have tried this but something is wrong:

function convertBinary() {
    var output = document.getElementById("outputBinary");
    var input = document.getElementById("inputBinary").value;
    output.value = "";
    for (i=0; i < input.length; i++) {
        output.value +=input[i].charCodeAt(0).toString(2);
    }
}

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++) {
        output.value +=input[i].charCodeAt(0).toString(2);
    }
}
</script>
</head>
<body>
<center>
<div class="container">
    <span class="main">Binary Converter</span><br>
    <input type="text" onKeyUp="convertBinary()" id="inputBinary" class="inputBinary" placeholder="Enter Text"><br>
    <input type="text" id="outputBinary" class="outputBinary" readonly>
</div>
</center>
</body>
</html>

Any help will be greatly appreciated. Thanks, Omar.

2
  • Well when I use a binary to text it gives me a error "Error: Malformed binary. Your binary code is must be divisible by 8". Commented Jan 25, 2014 at 14:35
  • I don't know how error shows on your screen. In my system it works perfectly on chrome and firefox. Commented Jan 25, 2014 at 14:37

1 Answer 1

2
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;
    }
}

You welcome my friend

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you very much! I appreciate it very much!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.