Javascript \x转义 [英] Javascript \x escaping

查看:190
本文介绍了Javascript \x转义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经看到其他一些程序有这样的东西:

  var string ='\x32\ x20\x60\x78\x6e\x7a\x9c\x89' ; 

我不得不尝试用数字和字母来提取,以找到我想显示的文字。

我想知道是否有一个函数可以找到字符串的 \x 转义,如 string。 toUpperCase()在JS中。我正在使用处理 JS的问题,但它将可以使用其他编程语言来找到 \x 的ASCII。

解决方案

如果您有一个要转义的字符串,可以使用 String.prototype.charCodeAt()



如果你有具有转义的代码,您可以评估它们以获取原始字符串。如果是字符串转义字符串,则可以使用 String.fromCharCode()




  • 如果你具有'\x32\x20\x60\x78\x6e\x7a\x9c\x89'并且要2 `xnz'然后

     '\x32\x20\x60\x78\ x6e\x7a\x9c\x89'==2`xnz


  • 如果您有'\\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89 '这是一个文字字符串,值为 \x32\x20\x60\x78\x6e\x7a\x9c\x89 然后您可以通过将每对十六进制数字的十进制值传递到 String.prototype.fromCharCode()

     '\\\ \\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89'.replace(/ \\x([0 -9a-f] {2})/ g,function(_,pair){
    return String.fromCharCode(parseInt(pair,16));
    })

    或者,如果您可以确定安全性,则 eval 输入和性能不重要 1



    $ $ $ $ $ $ $ \x9c\\x89')

    请注意 嵌套在输入字符串周围的'中。



    如果你知道这是一个程序,它来自一个可靠的来源,你可以直接使用 eval ,这不会给你ASCII,而是执行程序本身。

      eval('\ \x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89')

    请注意,您提供的输入不是程序,eval调用失败。


  • 如果您有2`xnz并且想要'\x32\x20\x60\\ \\ x78\x6e\x7a\x9c\x89'然后

     2` xnz.split('')。map(function(e){
    return'\\x'+ e.charCodeAt(0).toString(16);
    })。join('')



I've seen a few other programs that have something like this:

var string = '\x32\x20\x60\x78\x6e\x7a\x9c\x89';

And I had to try to fiddle with the numbers and letters, to find the text I wanted to display.
I'm wondering if there is a function to find the \x escape of a string, like string.toUpperCase() in JS. I'm using JS, but it will be okay for me to use other programming languages to find the ASCII for \x.

解决方案

If you have a string that you want escaped, you can use String.prototype.charCodeAt()

If you have the code with escapes, you can just evaluate them to get the original string. If it's a string with literal escapes, you can use String.fromCharCode()

  • If you have '\x32\x20\x60\x78\x6e\x7a\x9c\x89' and want "2 `xnz" then

    '\x32\x20\x60\x78\x6e\x7a\x9c\x89' == "2 `xnz"
    

  • If you have '\\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89' which is a literal string with the value \x32\x20\x60\x78\x6e\x7a\x9c\x89 then you can parse it by passing the decimal value of each pair of hex digits to String.prototype.fromCharCode()

    '\\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89'.replace(/\\x([0-9a-f]{2})/g, function(_, pair) {
      return String.fromCharCode(parseInt(pair, 16));
    })
    

    Alternatively, eval is an option if you can be sure of the safety of the input and performance isn't important1.

    eval('"\\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89"')
    

    Note the " nested in the ' surrounding the input string.

    If you know it's a program, and it's from a trusted source, you can eval the string directly, which won't give you the ASCII, but will execute the program itself.

    eval('\\x32\\x20\\x60\\x78\\x6e\\x7a\\x9c\\x89')
    

    Note that the input you provided is not a program and the eval call fails.

  • If you have "2 `xnz" and want '\x32\x20\x60\x78\x6e\x7a\x9c\x89' then

    "2 `xnz".split('').map(function(e) {
      return '\\x' + e.charCodeAt(0).toString(16);
    }).join('')
    

这篇关于Javascript \x转义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆