Convert binary to text using javascript

How can I convert binary to text using JavaScript? I already did this convert the text to binary, but is there a way to do this the other way around?

Here is my code:

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; } } 
 <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script> <center> <div class="container"> <span class="main">Binary Converter</span><br> <textarea autofocus class="inputBinary" id="inputBinary" onKeyUp="convertBinary()"></textarea> <textarea class="outputBinary" id="outputBinary" readonly></textarea> <div class="about">Made by <strong>Omar</strong></div> </div> </center> 

Any help would be appreciated.

Thanks, Omar.

+10
javascript html
source share
15 answers

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

+21
source share

I recently completed an exercise using a for loop. Hope this is helpful:

 function binaryAgent(str) { var newBin = str.split(" "); var binCode = []; for (i = 0; i < newBin.length; i++) { binCode.push(String.fromCharCode(parseInt(newBin[i], 2))); } return binCode.join(""); } binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100'); //translates to "Aren't" 

EDIT: by learning more JavaScript, I was able to shorten the solution:

 function binaryAgent(str) { var binString = ''; str.split(' ').map(function(bin) { binString += String.fromCharCode(parseInt(bin, 2)); }); return binString; } binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100'); //translates to "Aren't" 
+21
source share

I know it's late, I'm just throwing my 2 cents to help the community. I came across what I wanted Binary to convert to text, and this is what I came up with.

Hope this helps someone here.

 function binaryToWords(str) { if(str.match(/[10]{8}/g)){ var wordFromBinary = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){ return String.fromCharCode(parseInt(fromBinary, 2) ); }).join(''); return console.log(wordFromBinary); } } binaryToWords('01000011 01101111 01100110 01100110 01100101 01100101 00100000 01101001 01110011 00100000 01100011 01101111 01101100 01100100 '); 
+7
source share

Similar to another answer if someone is still looking for it. the first split returns a list of strings, each of which represents a binary character .

Then we call the display on each of these lines, for example, "11001111" or something else, and return fromCharCode for this element with the insert parseInt. Then put .join () on the total return value, and it should work.

 function binaryAgent3(str) { return str.split(" ").map(function(elem) { return String.fromCharCode(parseInt(elem, 2)); }).join("") } 

Original issue: http://www.freecodecamp.com/challenges/binary-agents

+4
source share

Here is the code I wrote that converts binary to string. The only difference is it is shorter and depends on the built-in JS functions.

 function binarytoString(str) { return str.split(/\s/).map(function (val){ return String.fromCharCode(parseInt(val, 2)); }).join(""); } 
+2
source share

How about this?

 function binaryAgent(str) { var splitStr = str.split(" "); var newVar = splitStr.map(function(val) { return String.fromCharCode(parseInt(val,2).toString(10)); }); str = newVar.join(""); return str; } binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); // should return "Aren't bonfires fun!?" 
+2
source share

if you are looking for 1 line solution.

 function binary(str) { return str.split(/\s/g).map((x) => x = String.fromCharCode(parseInt(x, 2))).join(""); } //returns "one line" binary("01101111 01101110 01100101 00100000 01101100 01101001 01101110 01100101"); 
+2
source share

This should work, and you don't need to give it a β€œpretty” binary.

 function binaryToString(binary) { return binary.replace(/[01]{8}/g, function(v){ return String.fromCharCode(parseInt(v, 2)); }); } 
+1
source share

My solution is very similar to the map, only I used the shortcut. Break it into an array, use the shortcut to convert the characters and add them to a new array and merge the new array.

 function binaryAgent(str) { var sentence = str.split(" ").reduce(function(x, y){ x.push(String.fromCharCode(parseInt(y, 2))); return x; }, []).join(''); return sentence; } 
0
source share

Another similar answer if someone is looking for this

 function binaryAgent(str) { var strArray = str.split(" "); var text =""; for(var i = 0 ; i<strArray.length ; i++){ var char = parseInt(strArray[i],2).toString(10); char = String.fromCharCode(char); text += char; } return text; } binaryAgent("01101101 01100101 01110110"); 
0
source share

The problem that I found in every provider-oriented solution was that if the binary string is not "pretty printed", it will not convert it to a string:

 function binaryAgent(str) { var binString = ''; str.split(' ').map(function(bin) { binString += String.fromCharCode(parseInt(bin, 2)); }); return binString; } // This displays "Aren't" binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100'); //But this not binaryAgent('010000010111001001100101011011100010011101110100'); 

A quick solution is to remove all spaces from the string (if there are any, otherwise the split command does not work) every 8 characters:

 function binaryAgent(str) { // Removes the spaces from the binary string str = str.replace(/\s+/g, ''); // Pretty (correct) print binary (add a space every 8 characters) str = str.match(/.{1,8}/g).join(" "); var binString = ''; str.split(' ').map(function(bin) { binString += String.fromCharCode(parseInt(bin, 2)); }); return binString; } // Both display "Aren't" binaryAgent('01000001 01110010 01100101 01101110 00100111 01110100'); binaryAgent('010000010111001001100101011011100010011101110100'); 
0
source share

For those who prefer forfor () over loops, the following also works:

  function binaryToHuman(str) { // split string into an array so we can loop through it var newStr=str.split(" "); // declare a new array to later push "translated" values into var sArr=[]; // loop through binary array, translate and push translated values into the new array newStr.forEach(function(item){ sArr.push(String.fromCharCode(parseInt(item,2))); }) // join the array back into a string return sArr.join(""); } console.log(binaryToHuman("01001001 00100000 01101100 01101111 01110110 01100101 00100001")); // returns: // I love! 
0
source share

My solution just uses a regular expression to split bytes into an array, iterates over each byte, converts the binary to ascii and turns it into a string at the end

 function binaryAgent(str) { //Splits into an array var re = /\s/; var newArr=str.split(re) var answerArr =[]; //Decimal Conversion for (let i=0; i<newArr.length; i++){ answerArr.push(String.fromCharCode(parseInt(newArr[i],2))); } return answerArr.join(''); } binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); 
0
source share

This is also an operation that you can delegate to the String prototype, although it is rarely recommended to update the global type of an object.

 /** Returns a converted binary string to text */ String.prototype.binaryToText = function() { return this .match(/.{1,8}/g) .join(' ') .split(' ') .reduce((a, c) => a += String.fromCharCode(parseInt(c, 2)), ''); } const result = '01110101011100000010000001110110011011110111010001100101'.binaryToText(); console.log(result); 
0
source share

If you know that only a binary file is transferred, you can use this function, which has only one simple line:

 function binaryAgent(str) { return str.split(" ").map(input => String.fromCharCode(parseInt(input,2).toString(10))).join(""); } // Calling the function binaryAgent("01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"); 
0
source share

All Articles