字符串转utf-8

java

String strInput = "测试:";
StringBuffer output = new StringBuffer();
System.out.println("\"" + strInput + "\" 的utf8编码:");
for (int i = 0; i < strInput.length(); i++) {
    //output.append("&#x" + Integer.toString(strInput.charAt(i), 16)+";");
    output.append("\\u" + Integer.toString(strInput.charAt(i), 16)+";");
}
System.out.println(output);

js

var UTFTranslate = {
    Change: function (pValue) {
        return pValue.replace(/[^\u0000-\u00FF]/g, function ($0) {
            return escape($0).replace(/(%u)(\w{4})/gi, "&#x$2;")
        });
    },
    ReChange: function (pValue) {
        return unescape(pValue.replace(/&#x/g, '%u').replace(/\\u/g, '%u').replace(/;/g, ''));
    }
};

你可能感兴趣的:(字符串转utf-8)