开发者

Does String.fromCharCode(decimal value) in javascript supports extended characters also

开发者 https://www.devze.com 2023-01-04 06:01 出处:网络
I am using function String.fromCharCode(decimal value), and passing a decimal value to it.开发者_C百科

I am using function String.fromCharCode(decimal value), and passing a decimal value to it.

开发者_C百科

its working fine in terms of English characters, but when i m trying the same to decode for the Japanese characters, it gives me some arbit characters.

can anyone tell me does String.fromCharCode(decimal value) supports extended characters.


No, it doesn't support characters that use two surrogates. MDC has a utility function that is meant to handle this:

// 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);
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消