以下似乎不正确
"🚀".charCodeAt(0); // returns 55357 in both Firefox and Chrome
这是一个名为 ROCKET (U+1F680) 的 Unicode 字符,小数点应该是 128640。
这是针对我正在编写的 unicode 应用程序。似乎来自 unicode 6 的大多数但不是所有字符都停留在 55357。
我该如何解决?谢谢。
最佳答案
JavaScript 使用 UTF-16 编码;见this article详情:
Characters outside the BMP, e.g. U+1D306 tetragram for centre (𝌆), can only be encoded in UTF-16 using two 16-bit code units: 0xD834 0xDF06. This is called a surrogate pair. Note that a surrogate pair only represents a single character.
The first code unit of a surrogate pair is always in the range from 0xD800 to 0xDBFF, and is called a high surrogate or a lead surrogate.
The second code unit of a surrogate pair is always in the range from 0xDC00 to 0xDFFF, and is called a low surrogate or a trail surrogate.
您可以像这样解码代理对:
codePoint = (text.charCodeAt(0) - 0xD800) * 0x400 + text.charCodeAt(1) - 0xDC00 + 0x10000
完整代码可以在Mozilla documentation for charCodeAt中找到.
关于Javascript "🚀".charCodeAt(0) 停留在 55357?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15181809/