js conversion Encode bytecode Chinese character conversion

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;
}

Result test: encode('zhongguo')
"zhongguo"

encode('China')
"[4e2d][56fd]"

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324779908&siteId=291194637