Javascript "🚀".charCodeAt(0) 停留在 55357?

标签 javascript unicode

以下似乎不正确

"🚀".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/

相关文章:

javascript - d3js svg viewBox 不允许计算 window.innerWidth - 170

JavaScript 语法错误,缺少 ";"

javascript - ASP.NET 中的 Jquery 和 Ajax 如何用模型更新 div

swift - PURE Swift 中的十六进制字符串到字符

javascript - 将 Algolia Instantsearch.js 与 Autocomplete.js 同步

javascript - 为什么要分派(dispatch)组件而不是操作文件?

php - 比较 PHP 中的 Unicode 字符

windows - 如何更改 Windows Unicode 语言环境/语言的默认字体

python - Selenium Python 2.7 - 断言非 ascii 字符

python - 以二进制方式将unicode字符写入文件