将数字转换为 Unicode 符号

发布于 2024-12-12 22:22:13 字数 260 浏览 2 评论 0原文

我必须把从 A 到 Z 的字母分别打印出来。所以我尝试了以下方法:

for(var i = 65; i < 91; i++) {
     $('#alphabet').append('<div class="letter">' + '%' + i + '</div>');
}

我的想法是使用字母的十进制数字(例如:65 - A)通过循环轻松打印它们。这是可能的还是我必须使用数组?

此致。

I have to print out the letters from A to Z each for itself. So I tried the following:

for(var i = 65; i < 91; i++) {
     $('#alphabet').append('<div class="letter">' + '%' + i + '</div>');
}

My idea is to use the decimal numbers of the letters (for example: 65 - A) to easily print them via loop. Is this possible or do I have to use an array?

Best regards.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

葬花如无物 2024-12-19 22:22:13

您可以使用 String.fromCharCode 将字符代码转换为字符串。

You can use String.fromCharCode to convert a character code to string.

像极了他 2024-12-19 22:22:13

对于这种用途(打印从 A 到 Z 的字母),String.fromCharCode 就足够了;但是,问题标题指定了 Unicode。如果您想要转换 Unicode 代码(如果您是通过搜索来到这里的,则可能会如此),您需要使用 String.fromCodePoint

请注意,此功能是 ES6/ES2015 中的新功能,您可能需要使用转译器或 polyfill 才能在旧版浏览器中使用它。

For this use (printing letters from A to Z), String.fromCharCode is sufficient; however, the question title specifies Unicode. If you are looking to convert Unicode codes (and if you came here from search, you might), you need to use String.fromCodePoint.

Note, this function is new to ES6/ES2015, and you may need to use a transpiler or polyfill to use it in older browsers.

一影成城 2024-12-19 22:22:13

String.fromCharCode

[...Array(91-65)].map((_, i)=>console.log(String.fromCharCode(i+65)))

String.fromCharCode(...codes: number[]): string 属于 UTF-16 代码单元的数字序列。

因此,对于大于 0xFFFF 的 Unicode 代码点,代理对 (U+需要 D800 到 U+DFFF(代理) 来显示它们,

您可以使用以下函数来实现此目的:

function fromCodePoint(codePoint) {
  if (codePoint > 0xFFFF) {
    codePoint -= 0x10000
    // const high = 0xD800 + (codePoint >> 10)
    // const low = 0xDC00 + (codePoint & 0x3FF) // 0x3FF 0011_1111_1111
    // return String.fromCharCode(high, low)
    return String.fromCharCode(
      0xD800 + (codePoint >> 10), 
      0xDC00 + (codePoint & 0x3FF)
    )
  }
  return String.fromCharCode(codePoint)
}

console.log(fromCodePoint(0x4E00))
console.log(fromCodePoint(0x1FA80)) // Yo-Yo // https://www.compart.com/en/unicode/U+1fa80

String.fromCharCode

[...Array(91-65)].map((_, i)=>console.log(String.fromCharCode(i+65)))

String.fromCharCode(...codes: number[]): string A sequence of numbers that are UTF-16 code units.

Therefore, for Unicode code points greater than 0xFFFF, surrogate pairs (U+D800 to U+DFFF (surrogates) are needed to display them.

You can use the following function to achieve this:

function fromCodePoint(codePoint) {
  if (codePoint > 0xFFFF) {
    codePoint -= 0x10000
    // const high = 0xD800 + (codePoint >> 10)
    // const low = 0xDC00 + (codePoint & 0x3FF) // 0x3FF 0011_1111_1111
    // return String.fromCharCode(high, low)
    return String.fromCharCode(
      0xD800 + (codePoint >> 10), 
      0xDC00 + (codePoint & 0x3FF)
    )
  }
  return String.fromCharCode(codePoint)
}

console.log(fromCodePoint(0x4E00))
console.log(fromCodePoint(0x1FA80)) // Yo-Yo // https://www.compart.com/en/unicode/U+1fa80

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文