elaine_fu666
2015-10-15 03:15
浏览 6.1k
已结题

lua中编码转换 utf16转utf8,unicode转utf8

lua中怎么从utf16转到utf8 ?
从网上找到个js的实现方法,但是不知道参数是什么,

 function utf16ToUtf8(s){  
    if(!s){  
        return;  
    }  

    var i, code, ret = [], len = s.length;  
    for(i = 0; i < len; i++){  
        code = s.charCodeAt(i);  
        if(code > 0x0 && code <= 0x7f){  
            //单字节  
            //UTF-16 0000 - 007F  
            //UTF-8  0xxxxxxx  
            ret.push(s.charAt(i));  
        }else if(code >= 0x80 && code <= 0x7ff){  
            //双字节  
            //UTF-16 0080 - 07FF  
            //UTF-8  110xxxxx 10xxxxxx  
            ret.push(  
                //110xxxxx  
                String.fromCharCode(0xc0 | ((code >> 6) & 0x1f)),  
                //10xxxxxx  
                String.fromCharCode(0x80 | (code & 0x3f))  
            );  
        }else if(code >= 0x800 && code <= 0xffff){  
            //三字节  
            //UTF-16 0800 - FFFF  
            //UTF-8  1110xxxx 10xxxxxx 10xxxxxx  
            ret.push(  
                //1110xxxx  
                String.fromCharCode(0xe0 | ((code >> 12) & 0xf)),  
                //10xxxxxx  
                String.fromCharCode(0x80 | ((code >> 6) & 0x3f)),  
                //10xxxxxx  
                String.fromCharCode(0x80 | (code & 0x3f))  
            );  
        }  
    }  

    return ret.join('');  
} 
  • 写回答
  • 好问题 提建议
  • 追加酬金
  • 关注问题
  • 收藏
  • 邀请回答

相关推荐 更多相似问题