js 转换Encode字节码 汉字转换

function encode(text) {
    var me = this;
    if (text == null) {
        return "";
    }
    var newText = "";
    for (var i = 0; i < text.length; i++) {
        var code = text.charCodeAt (i);
        if (code >= 128 || code == 91 || code == 93) {  //91 is "[", 93 is "]".
            newText += "[" + code.toString(16) + "]";
        } else {
            newText += text.charAt(i);
        }
    }
    return newText;
}

结果测试:encode('zhongguo')
"zhongguo"

encode('中国')
"[4e2d][56fd]"

 

posted @ 2018-04-23 09:53  wc_nan  阅读(764)  评论(0编辑  收藏  举报