Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
159
rated 0 times [  161] [ 2]  / answers: 1 / hits: 17376  / 13 Years ago, fri, august 19, 2011, 12:00:00

To express, for example, the character U+10400 in JavaScript, I use uD801uDC00 or String.fromCharCode(0xD801) + String.fromCharCode(0xDC00). How do I figure that out for a given unicode character? I want the following:



var char = getUnicodeCharacter(0x10400);


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


More From » unicode

 Answers
23

Based on the wikipedia article given by Henning Makholm, the following function will return the correct character for a code point:



function getUnicodeCharacter(cp) {

if (cp >= 0 && cp <= 0xD7FF || cp >= 0xE000 && cp <= 0xFFFF) {
return String.fromCharCode(cp);
} else if (cp >= 0x10000 && cp <= 0x10FFFF) {

// we substract 0x10000 from cp to get a 20-bits number
// in the range 0..0xFFFF
cp -= 0x10000;

// we add 0xD800 to the number formed by the first 10 bits
// to give the first byte
var first = ((0xffc00 & cp) >> 10) + 0xD800

// we add 0xDC00 to the number formed by the low 10 bits
// to give the second byte
var second = (0x3ff & cp) + 0xDC00;

return String.fromCharCode(first) + String.fromCharCode(second);
}
}

[#90522] Thursday, August 18, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
emerymariamm

Total Points: 276
Total Questions: 97
Total Answers: 99

Location: Comoros
Member since Sun, Dec 13, 2020
4 Years ago
;