public static void main(String[] args) throws UnsupportedEncodingException {
// 获取unicode码的几种方式
// 第一种
System.err.println("第一种-----toCodePoints");
String test = "a汉字";
int[] asds = StringUtils.toCodePoints(test);
for (int asd : asds) {
System.err.println("int整型:"+asd);
String x = Integer.toHexString(asd);
if (x.length() <= 2) {
x = "\\u00" + x;
} else {
x = "\\u" + x;
}
System.err.println("unicode码" + x);
}
//第二种
System.err.println("第二种-----getBytes");
byte[] bytes = test.getBytes("unicode");
List<Object> list = new ArrayList<>();
for (byte aByte : bytes) {
System.err.println("byte字节:"+aByte);
String x = Integer.toHexString(aByte);
if ((-2 != aByte) && (-1 != aByte)) {
list.add(x);
}
}
for (int i = 0; i < list.size(); i = i + 2) {
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(list.get(i));
stringBuilder.append(list.get(i + 1));
if (stringBuilder.length() < 4) {
for (int j = 0; j < 4 - stringBuilder.length(); j++) {
stringBuilder.insert(0, "0");
}
}
System.err.println("unicode码" + "\\u".concat(stringBuilder.toString()));
}
// 第三种
System.err.println("第三种-----toCharArray");
char[] chars = test.toCharArray();
for (char aChar : chars) {
System.err.println("char字符:"+aChar);
String x = Integer.toHexString(aChar);
if (x.length() <= 2) {
x = "\\u00" + x;
} else {
x = "\\u" + x;
}
System.err.println("unicode码" + x);
}
}
输出结果
第一种-----toCodePoints
int整型:97
unicode码\u0061
int整型:27721
unicode码\u6c49
int整型:23383
unicode码\u5b57
第二种-----getBytes
byte字节:-2
byte字节:-1
byte字节:0
byte字节:97
byte字节:108
byte字节:73
byte字节:91
byte字节:87
unicode码\u0061
unicode码\u6c49
unicode码\u5b57
第三种-----toCharArray
char字符:a
unicode码\u0061
char字符:汉
unicode码\u6c49
char字符:字
unicode码\u5b57