javascript中的String.fromCharCode(十进制值)是否也支持扩展字符

时间:2010-06-17 05:44:11

标签: javascript string

我正在使用函数String.fromCharCode(十进制值),并将十进制值传递给它。

它在英文字符方面工作正常,但是当我尝试解码日文字符时,它给了我一些仲裁字符。

任何人都可以告诉我String.fromCharCode(十进制值)是否支持扩展字符。

1 个答案:

答案 0 :(得分:0)

不,它不支持使用两个代理的字符。 MDC有一个实用函数,用于处理这个问题:

// String.fromCharCode() alone cannot get the character at such a high code point
// The following, on the other hand, can return a 4-byte character as well as the 
//   usual 2-byte ones (i.e., it can return a single character which actually has 
//   a string length of 2 instead of 1!)
alert(fixedFromCharCode(0x2F804)); // or 194564 in decimal

function fixedFromCharCode (codePt) {
    if (codePt > 0xFFFF) {
        codePt -= 0x10000;
        return String.fromCharCode(0xD800 + (codePt >> 10), 0xDC00 +
(codePt & 0x3FF));
    }
    else {
        return String.fromCharCode(codePt);
    }
}