我如何使用python将大量的unicode转换为char,使其看起来像字符串?

时间:2015-03-04 12:40:23

标签: javascript python unicode decode

我在javascript中得到了它:

eval(String.fromCharCode(102,117,110,99,116,105,111,110,32,99,104,101,99,107,40,41,123,13,10,09,118,97,114,32,97,32,61,32,39,100,52,103,39,59,13,10,09,105,102,40,100,111,99,117,109,101,110,116,46,103,101,116,69,108,101,109,101,110,116,66,121,73,100,40,39,116,120,116,39,41,46,118,97,108,117,101,61,61,97,41,123,13,10,09,09,119,105,110,100,111,119,46,108,111,99,97,116,105,111,110,46,104,114,101,102,61,97,43,34,46,112,104,112,34,59,13,10,09,125,101,108,115,101,123,13,10,09,09,97,108,101,114,116,40,34,23494,30721,38169,35823,34,41,59,13,10,09,125,13,10,125));

我想将此unicode转换为字符串,以便我可以知道它运行的代码。

请帮我转换为python

2 个答案:

答案 0 :(得分:2)

在Python 2中,使用unichr() function将整数转换为Unicode代码点;在Python 3中,将unichr()替换为chr()

演示:

>>> from ast import literal_eval
>>> s = """eval(String.fromCharCode(102,117,110,99,116,105,111,110,32,99,104,101,99,107,40,41,123,13,10,09,118,97,114,32,97,32,61,32,39,100,52,103,39,59,13,10,09,105,102,40,100,111,99,117,109,101,110,116,46,103,101,116,69,108,101,109,101,110,116,66,121,73,100,40,39,116,120,116,39,41,46,118,97,108,117,101,61,61,97,41,123,13,10,09,09,119,105,110,100,111,119,46,108,111,99,97,116,105,111,110,46,104,114,101,102,61,97,43,34,46,112,104,112,34,59,13,10,09,125,101,108,115,101,123,13,10,09,09,97,108,101,114,116,40,34,23494,30721,38169,35823,34,41,59,13,10,09,125,13,10,125));"""
>>> codes = literal_eval(s.split('(', 2)[-1].rsplit(')', 2)[0].replace(',0', ','))
>>> print u''.join(unichr(c) for c in codes)
function check(){
    var a = 'd4g';
    if(document.getElementById('txt').value==a){
        window.location.href=a+".php";
    }else{
        alert("密码错误");
    }
}

请注意,我必须用09替换9整数文字; Python 3中的前导零是语法错误,在Python 2中它们被解释为八进制文字(并且09不是有效的八进制数)。我使用ast.literal_eval() function将字符串表示转换为整数列表。

答案 1 :(得分:0)

如果这是从ord(或等效的)获得的值列表,您可以使用chr来反转该过程。

>>> ''.join(chr(i) for i in l)
'function check(){\r\n\tvar a = \'d4g\';\r\n\tif(document.getElementById(\'txt\').value==a){\r\n\t\twindow.location.href=a+".php";\r\n\t}else{\r\n\t\talert("密码错误");\r\n\t}\r\n}'

在格式化代码中,此字符串为

function check(){
    var a = 'd4g';
    if(document.getElementById('txt').value == a){
        window.location.href= a + ".php";
    }
    else{
        alert("密码错误");
    }
}