if (codePoint <= 0xffff) { returnu(codePoint); } codePoint -= 0x10000;
// Shift right to get to most significant 10 bits var leadSurrogate = 0xd800 + (codePoint >> 10);
// Mask to get least significant 10 bits var tailSurrogate = 0xdc00 + (codePoint & TEN_BITS);
returnu(leadSurrogate) + u(tailSurrogate); }
使用示例
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// using codePointAt, it's easy to go from emoji // to decimal and back. // Emoji to decimal representation "😀".codePointAt(0) > 128512;
// Decimal to emoji String.fromCodePoint(128512) > "😀";
// going from emoji to hexadecimal is a little // bit trickier. To convert from decimal to hexadecimal, // we can use toUTF16. // Decimal to hexadecimal toUTF16(128512) > "\uD83D\uDE00";