【问题标题】:String.fromCharCode(132) returns empty stringString.fromCharCode(132) 返回空字符串
【发布时间】:2021-06-26 16:18:32
【问题描述】:

我正在尝试创建一个加密系统,该系统获取字符串中每个字符的字符代码,然后将函数应用于它们以创建新的字符代码。然后,从这些字符代码中创建字符串。每件事都是成功的,但我注意到当在132 上调用String.fromCharCode 时,它返回一个空字符串。有没有办法解决这个问题?

我试过了:

'\u{132}' // works but '\u{132}.charCodeAt(0)' returns 306

【问题讨论】:

    标签: javascript string unicode


    【解决方案1】:

    这是因为您在两个不同的数字系统中工作。 charCode 是十进制的,但\u 表示法使用十六进制。 306是十六进制\u{132}的十进制表示:

    console.log('\u{132}'.charCodeAt(0)) // returns in decimal
    console.log(parseInt(132, 16)) // convert hex to decimal
    
    console.log('\u{132}') // the character represented in hex
    console.log(String.fromCharCode(306)) // and represented in decimal

    String.fromCharCode(132) 实际上返回一个control code,而不是一个空字符串。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-27
      • 2020-07-11
      • 2015-05-25
      • 2011-05-09
      • 1970-01-01
      • 2012-07-20
      • 2011-12-15
      • 2020-11-23
      相关资源
      最近更新 更多