【问题标题】:Why does String.fromCharCode(0xd800) to String.fromCharCode(0xdfff) return the replacement character?为什么 String.fromCharCode(0xd800) 到 String.fromCharCode(0xdfff) 返回替换字符?
【发布时间】:2021-03-18 04:53:34
【问题描述】:

为什么会这样:

> String.fromCharCode(0xd7FF)
'퟿'
> String.fromCharCode(0xd800)
'�'
> String.fromCharCode(0xdffe) // (and everything in between)
'�'
> String.fromCharCode(0xdfff)
'�'
> String.fromCharCode(0xe000)
''

DFFF₁₆ 是 55296₁₀。我使用String.fromCodePoint() 得到相同的结果。

【问题讨论】:

标签: javascript node.js unicode character-encoding codepoint


【解决方案1】:

代码点 U+D800 到 U+DFFF 保留用于surrogates 的 UTF-16 编码。实际上,这些是从不单独有效的字符——它们总是以代理对的形式出现——高代理后跟低代理。 (令人困惑的是,“高代理”范围是 U+D800 到 U+DBFF 的范围,而“低代理”范围是 U+DC00 到 U+DFFF 的范围。)

这对字符在 UTF-16 中组合以表示基本多语言平面之外的单个字符。

在 UTF-16 中的这个特殊含义之外,这些不是有效字符。所以String.fromCharCode 基本上说“你没有提供有效的字符串数据”并使用 Unicode 替换字符是合理的。

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-10
    • 1970-01-01
    • 2011-01-05
    相关资源
    最近更新 更多