Binary to String in JavaScript

Viewed 20049

I have created a JavaScript program to convert a string into binary.

Input: StackOverflow
Output: 1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111 

Now I want to convert that binary back into a string like below. Is there any possible way of doing this?

Input: 1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111 

Output StackOverflow

Thanks

3 Answers

Use String.fromCharCode() and parseInt( , 2) like this:

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const outputStr = binary
  // split string into an array of base 2 (binary) UTF-16 chars
  .split(' ')
  // map every binary char to a UTF-16 number of base 10
  // and then get the string representation of that UTF-16 number
  .map(bin => String.fromCharCode(parseInt(bin, 2)))
  // join the array back to a single string
  .join('');

console.log(outputStr);

  • String.fromCharCode(number) will return a String from a UTF-16 char code
  • parseInt(binary , 2) will transform a base 2 number string into a base 10 number

Edit:

As the String.fromCharCode() function accepts multiple chars as parameters, you could also use the Spread Operator (...) like so:

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const outputStr = String.fromCharCode(
  ...binary.split(' ').map(bin => parseInt(bin, 2))
)

console.log(outputStr);

Edit 2:

As this answer gets more traffic over time, I am going to also add the solution to do it the other way around too ... just in case:

const str = `StackOverflow`;

const outputStr = str.split('') // split in single chars
  .map(c => c.charCodeAt(0) // get the UTF-16 code (10 base)
             .toString(2)) // transform it back to a string (2 base)
  .join(' ') // make single string from array

console.log(outputStr);

str ="01000001 01110010 01100101 01101110 00100111 01110100 00100000 01100010 01101111 01101110 01100110 01101001 01110010 01100101 01110011 00100000 01100110 01110101 01101110 00100001 00111111"
    
    
function binaryAgent(str) {
      return str.split(' ').map(letter=>String.fromCharCode(parseInt(letter, 2))).join('')
     
}
    
console.log(binaryAgent(str))

  • First we use the .split() method to convert the string to array using the ' '(space character)

  • After that we use the .map() high-order loop method to iterate through each binary-code-letter

  • For each binary-code-letter we use String.fromCharCode() on parseInt('each letter',2)=> which converts the binary string to a number. The second argument "2" of the parseInt method which is called(radix) is to define which numeral system we want to use.

  • Finally we can join the manipulated array back together calling the join('') method but this time we don't use a space between

=======

const binary = `1010011 1110100 1100001 1100011 1101011 
1001111 1110110 1100101 1110010 1100110 
1101100 1101111 1110111`;

const inputStr = binary.split(' ');
const output = [];
inputStr.forEach(item => {output.push(String.fromCharCode(parseInt(item, 2)))});
console.log(output.join(''));

Related