推荐关注
Meteor
JSLint - a JavaScript code quality tool
jsFiddle
D3.js
WebStorm
推荐书目
JavaScript 权威指南第 5 版
Closure: The Definitive Guide
abcbuzhiming

JS 的 16 进制转义字符只能使用常量的模式吗,能用变量拼吗?

  •  
  •   abcbuzhiming · May 3, 2017 · 2546 views
    This topic created in 3305 days ago, the information mentioned may be changed or developed.
    在解码需求的范例里看到的这样的东西

    jschardet.detect("\xc3\xa0\xc3\xad\xc3\xa0\xc3\xa7\xc3\xa3")

    这串"\xc3\xa0\xc3\xad\xc3\xa0\xc3\xa7\xc3\xa3"是个 16 进制转义字符串,但是当我试图拼出这么个“字符串”的时候
    '\x' + 'c3' + '\x' + 'a0' + ....................
    转义就失效了,这种字符串只能以常量的方式定义吗?
    5 replies    2017-05-03 22:37:41 +08:00
    Mitt
        1
    Mitt  
       May 3, 2017
    转义字符 和 字符串 是不同的两个东西
    ----
    回答: 是的
    fds
        2
    fds  
       May 3, 2017
    String.fromCharCode(65, 66, 67); // "ABC"
    hxsf
        3
    hxsf  
       May 3, 2017
    楼主你在想什么=_=

    \xc3 是一个字符, 不是 \ + x + c + 3

    你可能需要 String.fromCharCode(195) // c3(hex) = 195(dec)
    hxsf
        4
    hxsf  
       May 3, 2017
    对了, 可能你还需要这个

    不推荐: '0xc3' - 0 // 195
    不推荐 es5: parseInt('0xc3') // 195
    推荐 es5: parseInt('c3', 16) // 195
    注意 es6: parseInt => Number.parseInt
    mingyun
        5
    mingyun  
       May 3, 2017
    @hxsf nice
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   1028 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 53ms · UTC 19:06 · PVG 03:06 · LAX 12:06 · JFK 15:06
    ♥ Do have faith in what you're doing.