假设我有一个字符串foo ="这是一个苹果"
等效的Unicode代码点将是
" \\x74\\x68\\x69\\x73.......... \\x61\\x70\\x70\\x6c\\x65
"
T h i s ............. a p p l e
如何从String foo转换
到
String" \\x74\\x68\\x69\\x73.......... \\x61\\x70\\x70\\x6c\\x65
"
答案 0 :(得分:1)
试试这个..
public static String generateUnicode(String input) {
StringBuilder b = new StringBuilder(input.length());
for (char c : input.toCharArray()) {
b.append(String.format("\\u%04x", (int) c));
}
return b.toString();
}
答案 1 :(得分:0)
这是一个用于转换的工作代码段:
public class HexTest {
public static void main(String[] args) {
String testStr = "hello日本語 ";
System.out.println(stringToUnicode3Representation(testStr));
}
private static String stringToUnicode3Representation(String str) {
StringBuilder result = new StringBuilder();
char[] charArr = str.toCharArray();
for (int i = 0; i < charArr.length; i++) {
result.append("\\u").append(Integer.toHexString(charArr[i] | 0x10000).substring(1));
}
return result.toString();
}
}
那个显示:
\ u0068 \ u0065 \ u006c \ u006c \ u006f \ u65e5 \ u672c \ u8a9e \ u0020
如果你想摆脱额外的零,你按照here所描述的那样详细说明。
通过传递"This is an apple"
来获取
\ U54 \ U68 \ U69 \ U73 \ U20 \ U69 \ U73 \ U20 \ U61 \ U6E \ U20 \ U61 \ U70 \ U70 \ U6C \ U65
使用:
private static String str2UnicodeRepresentation(String str) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < str.length(); i++) {
int cp = Character.codePointAt(str, i);
int charCount = Character.charCount(cp);
//UTF characters may use more than 1 char to be represented
if (charCount == 2) {
i++;
}
result.append(String.format("\\u%x", cp));
}
return result.toString();
}