中文和Unicode互相转化

来源:互联网 发布:开源cms php 编辑:程序博客网 时间:2024/06/06 07:27
Unicode转中文
String unicode = "\u6211\u7231\u7956\u56fd.mp3";
String result = new String(unicode.getBytes("UTF-8"), "UTF-8");
System.out.println(result);

结果:我爱祖国

中文转Unicode
String chinese = "我爱祖国";
StringBuffer unicode = new StringBuffer();
for (int i = 0; i < chinese.length(); i++) {
    Character character = chinese.charAt(i);
    unicode.append("\\u" + Integer.toHexString(character));
}
System.out.println(unicode.toString());

结果:\u6211\u7231\u7956\u56fd
1 0
原创粉丝点击