Created
September 21, 2017 02:43
-
-
Save allenhwkim/b6df7acf7e1c4f3dc2b0fbe5a9e84862 to your computer and use it in GitHub Desktop.
Return string formatted utf16 expression from a character
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| function toUTF16(codePoint) { | |
| var TEN_BITS = parseInt('1111111111', 2); | |
| function u(codeUnit) { | |
| return '\\u'+codeUnit.toString(16).toUpperCase(); | |
| } | |
| if (codePoint <= 0xFFFF) { | |
| return u(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); | |
| return u(leadSurrogate) + u(tailSurrogate); | |
| } | |
| //toUTF16('\xF6'.charCodeAt(0)) // "\uF6" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment