I have created a JavaScript test 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 have converted this binary code into 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 the problem is with function init() {output};
as output is not JavaScript, it is a string.
I have searched and searched, I found how to convert binary to a string, but I cannot find a way to convert a string to actual JavaScript.
can this be done?
You're looking for eval
. It explicitly invokes the compiler for you on a string and runs it as JavaScript.
eval("alert('hi');"); // evaluates the string and executes it as code
As an alternative, you can treat the code as a function body (with arguments) and call the Function
constructor on it.
var converted = var binary = convertBinary("...");
eval(converted); // run code
var init = Function(converted); // create a function you can later call with the code
I don't want to get into why you're converting stings this way - if it's an exercise it's good and fun. Keep in mind that files are already always in binary and being a text file is only a matter how we look at its contents and not what its actual contents is.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With