user3622460 user3622460 - 7 months ago 34
jQuery Question

Is there an easy way to go from binary to ACSII like charCodeAt goes from text to ASCII?

I have a variable of

joinMsg = "0,1,1,0,0,1,1,0";

I made it from an array

putMsg = [ "0", "1", "1", "0", "0", "1", "1", "0" ]
joinMsg = putMsg.join();

My goal now is to convert this to the ASCII number, (which would be "f"). I'm trying to basically take a binary number and go back to ASCII then text. I originally got this number by user charCodeAt to go from f to ASCII to binary.

Is there a simple way of converting back to text from a binary string?

I've tried praseInt like this...

var joinMsg = putMsg.join();
var back = String.fromCharCode(parseInt(joinMsg,2));

But back prints as a blank space, when I would expect it to print as 'f'

Added the answer given and then I added ("") to my join and it works! Thanks for the help


Well, you can go from a decimal number to a character with String.fromCharCode and from a binary number to a decimal integer with parseInt using the base 2.

String.fromCharCode(parseInt('01100110', 2)); //f