Expressing UTF-16 unicode characters in JavaScript

前端 未结 2 463
死守一世寂寞
死守一世寂寞 2020-12-09 11:02

To express, for example, the character U+10400 in JavaScript, I use \"\\uD801\\uDC00\" or String.fromCharCode(0xD801) + String.fromCharCode(0xDC00)

2条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-09 11:38

    How do I find 0xD801 and 0xDC00 from 0x10400?

    JavaScript uses UCS-2 internally. That’s why String#charCodeAt() doesn’t work the way you’d want it to.

    If you want to get the code point of every Unicode character (including non-BMP characters) in a string, you could use Punycode.js’s utility functions to convert between UCS-2 strings and UTF-16 code points:

    // String#charCodeAt() replacement that only considers full Unicode characters
    punycode.ucs2.decode('

提交回复
热议问题