最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

string - Javascript x escaping - Stack Overflow

programmeradmin2浏览0评论

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 processingJS, but it will be okay for me to use other programming languages to find the ASCII for \x.

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 processingJS, but it will be okay for me to use other programming languages to find the ASCII for \x.

Share Improve this question edited Dec 3, 2015 at 2:56 Vikrant 5,03618 gold badges51 silver badges74 bronze badges asked Dec 3, 2015 at 1:53 HarryJamesPotter27HarryJamesPotter27 1811 gold badge1 silver badge11 bronze badges 2
  • Those programs could (and should) just have used var string = '2 `xnz\x9c\x89';, and nothing would have been different. – Bergi Commented Dec 3, 2015 at 2:15
  • That is true, But I wasn't used to this until now, I usually just type the characters. – HarryJamesPotter27 Commented Dec 3, 2015 at 13:09
Add a comment  | 

1 Answer 1

Reset to default 15

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})/ig, 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('')
    
发布评论

评论列表(0)

  1. 暂无评论