我已经创建了一个JavaScript测试文件并将其转换为二进制。
在另一个JavaScript文件中,我已经将这个二进制代码转换为字符串,但是代码无法运行。
我知道问题出在
我已经搜索了很久,找到了如何将二进制转换为字符串的方法,但我找不到将字符串转换为实际JavaScript代码的方法。
这可以做到吗?
var newElement = document.createElement("h1");
var element = document.createTextNode("Hello World!");
newElement.appendChild(element);
document.body.appendChild(newElement);
转换为:
"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"
在另一个JavaScript文件中,我已经将这个二进制代码转换为字符串,但是代码无法运行。
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();
我知道问题出在
function init() {output};
,因为output不是JavaScript,而是一个字符串。我已经搜索了很久,找到了如何将二进制转换为字符串的方法,但我找不到将字符串转换为实际JavaScript代码的方法。
这可以做到吗?
eval
。 - hindmostnew Function(output)()
- nnnnnn