将二进制转换为JavaScript?/ 将字符串转换为JavaScript?

4
我已经创建了一个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代码的方法。
这可以做到吗?

2
我猜你在寻找eval - hindmost
new Function(output)() - nnnnnn
你可能还想调整一下返回和输出变量的顺序。现在它在到达 return 语句时就停止了,从未改变输出变量。此外,我也不确定你是否真的想要在第一次使用 console.log 时就进行返回。 - Verkade89
在控制台中,代码看起来与第一个代码块中的代码完全相同,它没有像字符串一样被包含在“”中,所以问题可能只是返回值的问题? - eddmcmxciii
你想要在控制台中使用console.log(js);,然后将输出赋值给js,并返回js。这样你就可以在控制台中看到代码了。你更新了输出变量,同时也返回了转换后的数据。但是你也可以删除输出变量,改用二进制变量。我的意思是当你执行var binary = convertBinary();时,binary会得到返回值。没有必要使用两个具有相同值的变量(output和binary),对吧? - Verkade89
显示剩余13条评论
5个回答

4
你需要使用 eval。它会将字符串传入编译器并作为 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

我不想深入解释为什么要这样转换字符串 - 如果这是一个练习,那就很好玩。请记住文件已经始终以二进制形式存在,而作为“文本文件”只是我们查看其内容的方式,而不是其实际内容。


1
因为 eval 会使代码变得非常缓慢。除非必须使用,否则不要使用它。在这种情况下,正确的方法是使用 new Function - Sebastian Nette
1
这正是我正在寻找的!正如@Verkade89所指出的那样,第7行的返回可能也存在问题。 - eddmcmxciii
2
@SebastianNette 不,它并不会。人们之所以说“eval 使代码变慢”,是因为他们在机械式地模仿多年前听到的东西。如果代码只运行一次,那么 new Functioneval 的性能将完全相同 - 差异在于作用域规则,在这个问题中无关紧要。 - Benjamin Gruenbaum
2
@SebastianNette 我不知道你从哪里得出这个结论的,我没有阅读所有JS运行时的源代码,但我确切知道,在V8中,JavaScriptCode和SpiderMonkey的性能完全相同。你确定你没有混淆吗?不过在这种情况下,性能并不重要,JIT也无法发挥作用。嗯。 - Benjamin Gruenbaum
4
@SebastianNette 对不起,如果我说错了话 - 你是故意用那个基准来嘲笑我吗?这是讽刺吗?我不明白。我尝试提供一个有用的答案,而且我认为在任何时候我都没有特别粗鲁... 不管怎样,如果我稍微改变一下那个基准(仍然不是一个好的基准,并且不能测量加法)http://jsfiddle.net/yn3pnsf1/ 你会注意到没有任何区别(因为嘿,我们现在不再测试范围查找)。 - Benjamin Gruenbaum
显示剩余6条评论

2
您可以使用eval函数,像这样:

eval(output)

或者Function构造函数,像这样:

var init = new Function(output) init()


我已经尝试过 eval(output),我已经尝试过 var init = new Function(output) 和我已经尝试过 new Function(eval(output))。但它仍然无法工作。我一直收到错误 Uncaught SyntaxError: Unexpected token ILLEGAL - eddmcmxciii

1
你可以使用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);
}

0

已解决!

二进制到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('');

}


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接