【前端】JS 实现 unicode 中文互转

来源:互联网 发布:2017淘宝客服工作计划 编辑:程序博客网 时间:2024/06/01 09:09

中文和 unicode 实现除了在服务器端实现,javascript 同样可以实现:

// 转为unicode 编码function encodeUnicode(str) {    var res = [];    for ( var i=0; i<str.length; i++ ) {res[i] = ( "00" + str.charCodeAt(i).toString(16) ).slice(-4);    }    return "\\u" + res.join("\\u");}// 解码function decodeUnicode(str) {    str = str.replace(/\\/g, "%");    return unescape(str);}


End .


0 0
原创粉丝点击