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 must be decimal. Otherwise, the input "0x42" will be parsed as hexadecimal rather than decimal.
EDIT: Just re-read the question. To go from binary to text, use parseInt (input, 2) .toString (10).
All of the above is for numbers only. For example, 4 β 0100 . If you want 4 β decimal 52 (its ASCII value), use String.fromCharCode() (see this answer ).
EDIT 2: for each query where everything fits, try the following:
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).
cxw
source share