我试图获取java转义码
示例:1F612
==> \ud83d\ude12
我试过了:
String toConvert = "\ud83d\ude12";
String result = "";
for(int x=0;x<toConvert.length();x++){
int codePoint = Character.codePointAt(toConvert, x);
String hexStr = Integer.toHexString(codePoint);
hexStr = formatUTF(hexStr);
result += hexStr;
}
System.out.println(result);
formatUTF函数:
public static String formatUTF(String hex){
String text = hex;
for(int x = 0; x<4-hex.length();x++)
text = "0"+text;
return "\\u"+text;
}
但是输出:
run:
\u1f612\ude12
注意:1F612
十六进制= 128530
整数
请帮忙。
答案 0 :(得分:1)
也许这澄清了一切。
for (int i = 0; i < toConvert.length(); ) {
int codePoint = Character.codePointAt(toConvert, i);
i += Character.charCount(codePoint);
System.out.printf("[%d] cp: %x%n", i, codePoint);
}
for (int i = 0; i < toConvert.length(); ++i) {
char ch = toConvert.charAt(i);
System.out.printf("[%d] c: %x%n", i, (int)ch);
}
它产生两个16位字符的单个代码点。
[2] cp: 1f612
[0] c: d83d
[1] c: de12
正如UTF-16 standard所说的那样。