Encoder

来源:互联网 发布:阿里云如何关闭网站 编辑:程序博客网 时间:2024/05/19 18:10


/**
 * 将中文编码转换
 * 
 * @author Administrator
 *
 */
public class Encoder {


public static String toUnicoder(String str) {
// 创建字符串缓冲区
StringBuilder builder = new StringBuilder();
// 将传入的字符串转为字符数组
char[] chars = str.toCharArray();
// 遍历字符数组
for (char ch : chars) {
if (ch < 256) {
builder.append(ch);
} else {
builder.append("\\u" + Integer.toHexString(ch));
}
}
return builder.toString();
}


public static void main(String[] args) {
String code = Encoder.toUnicoder("中国");
System.out.println(code);
}
}
原创粉丝点击