Is it possible to convert a string containing “high” unicode chars to an array consisting of dec values derived from utf-32 (“real”) codes?

我只是一个虾纸丫 提交于 2019-12-05 15:27:59

It looks like you have to decode surrogate pairs manually. For example:

function decodeUnicode(str) {
    var r = [], i = 0;
    while(i < str.length) {
        var chr = str.charCodeAt(i++);
        if(chr >= 0xD800 && chr <= 0xDBFF) {
            // surrogate pair
            var low = str.charCodeAt(i++);
            r.push(0x10000 + ((chr - 0xD800) << 10) | (low - 0xDC00));
        } else {
            // ordinary character
            r.push(chr);
        }
    }
    return r;
}

Complete code: http://jsfiddle.net/twQWU/

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!