中文转unicode

String str = "中文转unicode";
char[] utfBytes = str.toCharArray();
String unicodeBytes = "";
for (int i = 0; i < utfBytes.length; i++) {
String hexB = Integer.toHexString(utfBytes[i]);
if (hexB.length() <= 2) {
hexB = "00" + hexB;
}
unicodeBytes = unicodeBytes + "\\u" + hexB;
}
System.out.println(unicodeBytes);

posted @ 2019-09-05 17:30  it-ayong  阅读(257)  评论(0编辑  收藏  举报