Convert binary to javascript? / Convert string to javascript?

I created a test JavaScript file and converted it to binary.

var newElement = document.createElement("h1");
var element = document.createTextNode("Hello World!");
newElement.appendChild(element);
document.body.appendChild(newElement);

Converted to:

"01110110 01100001 01110010 00100000 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00100000 00111101 00100000 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100011 01110010 01100101 01100001 01110100 01100101 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101000 00100010 01101000 00110001 00100010 00101001 00111011 00001101 00001010 01110110 01100001 01110010 00100000 01100101 01101100 01100101 01101101 01100101 01101110 01110100 00100000 00111101 00100000 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100011 01110010 01100101 01100001 01110100 01100101 01010100 01100101 01111000 01110100 01001110 01101111 01100100 01100101 00101000 00100010 01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001 00100010 00101001 00111011 00001101 00001010 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101110 01100001 01110000 01110000 01100101 01101110 01100100 01000011 01101000 01101001 01101100 01100100 00101000 01100101 01101100 01100101 01101101 01100101 01101110 01110100 00101001 00111011 00001101 00001010 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100010 01101111 01100100 01111001 00101110 01100001 01110000 01110000 01100101 01101110 01100100 01000011 01101000 01101001 01101100 01100100 00101000 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101001 00111011"

In another JavaScript file, I converted this binary to a string, but the code does not run.

var output = "";
function convertBinary(str) { 
    if(str.match(/[10]{8}/g)){
        var js = str.match(/([10]{8}|\s+)/g).map(function(fromBinary){
            return String.fromCharCode(parseInt(fromBinary, 2) );
        }).join('');
        return console.log(js);
        output = js;
    }
}
var binary = convertBinary("01110110 01100001 01110010 00100000 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00100000 00111101 00100000 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100011 01110010 01100101 01100001 01110100 01100101 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101000 00100010 01101000 00110001 00100010 00101001 00111011 00001101 00001010 01110110 01100001 01110010 00100000 01100101 01101100 01100101 01101101 01100101 01101110 01110100 00100000 00111101 00100000 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100011 01110010 01100101 01100001 01110100 01100101 01010100 01100101 01111000 01110100 01001110 01101111 01100100 01100101 00101000 00100010 01001000 01100101 01101100 01101100 01101111 00100000 01010111 01101111 01110010 01101100 01100100 00100001 00100010 00101001 00111011 00001101 00001010 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101110 01100001 01110000 01110000 01100101 01101110 01100100 01000011 01101000 01101001 01101100 01100100 00101000 01100101 01101100 01100101 01101101 01100101 01101110 01110100 00101001 00111011 00001101 00001010 01100100 01101111 01100011 01110101 01101101 01100101 01101110 01110100 00101110 01100010 01101111 01100100 01111001 00101110 01100001 01110000 01110000 01100101 01101110 01100100 01000011 01101000 01101001 01101100 01100100 00101000 01101110 01100101 01110111 01000101 01101100 01100101 01101101 01100101 01101110 01110100 00101001 00111011");

function init() {output};
init();

I know that the problem is related function init() {output};, since the output is not JavaScript, it is a string.

I searched and searched, I found how to convert binary code to string, but I cannot find a way to convert the string to actual JavaScript.

Can this be done?

+4
source share
5 answers

You are looking for eval. It explicitly calls the compiler for you in the line and runs it as JavaScript.

eval("alert('hi');"); // evaluates the string and executes it as code

( ) Function .

var converted = var binary = convertBinary("...");
eval(converted); // run code
var init = Function(converted); // create a function you can later call with the code

, - . , , - , , , .

+4

eval, :

eval(output)

, :

var init = new Function(output) init()

+2

new Function . ​​:

var fn = new Function(output);
fn();

:

new Function(output)();

:

function(){
    var newElement = document.createElement("h1");
    var element = document.createTextNode("Hello World!");
    newElement.appendChild(element);
    document.body.appendChild(newElement);
}
+1

!

Binary JavaScript.

var bin = "some binary coded javascript"
var output = "";
function convertBinary(str) { 
    if(str.match(/[10]{8}/g)){
        var js = str.match(/([10]{8})/g).map(function(fromBinary){
            return String.fromCharCode(parseInt(fromBinary, 2) );
        }).join('');
        //console.log(js);
        output = js;
        return js;
    }
}
var binary = convertBinary(bin);
var init = new Function(output);
init();
0

function binaryAgent(str) {
  //every ' ' split the string and put it into an array
  var arr = str.split(' ');

  //for every value in the array move it to base 2 (unicode) and convert it
  for (var i in arr) {
    var c = parseInt(arr[i], 2);
    arr[i] = String.fromCharCode(c);
  }

  return str = arr.join('');

}
Hide result
0

All Articles