在JavaScript中表达UTF-16 unicode字符 [英] Expressing UTF-16 unicode characters in JavaScript
本文介绍了在JavaScript中表达UTF-16 unicode字符的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
例如,为了表达JavaScript中的字符U + 10400,我使用\ uD801 \ uDC00
或 String.fromCharCode (0xD801)+ String.fromCharCode(0xDC00)
。我如何计算出给定的unicode角色?我想要以下内容:
To express, for example, the character U+10400 in JavaScript, I use "\uD801\uDC00"
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);
如何找到 0xD801
和 0xDC00
来自 0x10400
?
推荐答案
根据Henning Makholm提供的维基百科文章,以下函数将返回代码的正确字符point:
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);
}
}
这篇关于在JavaScript中表达UTF-16 unicode字符的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文