Dhr*_*hok 2 java string unicode utf-8
我有一个字符串"1234567(Asics(アシックスワーキング))".它有unicode字符,有些是ASCII的一部分,有些则不是.java的作用是ASCII字符需要一个字节,其他unicode字符需要两个字节.
我的程序的某些部分无法以此格式处理字符串.所以我想将值编码为转义序列.
所以字符串
"1234567(Asics(アシックスワーキング))"
会映射到
"\ u0031\u0032\u0033\u0034\u0035\u0036\u0037\u0028\u0041\u0073\u0069\u0063\u0073\u0020\u0028\u30a2\u30b7\u30c3\u30af\u30b9\u30ef\u30fc\u30ad\u30f3\u30b0\u0029\u0020\u0029"
.
我写了这个函数来做到这一点: -
public static String convertToEscaped(String utf8) throws java.lang.Exception
{
char[] str = utf8.toCharArray();
StringBuilder unicodeStringBuilder = new StringBuilder();
for(int i = 0; i < str.length; i++){
char charValue = str[i];
int intValue = (int) charValue;
String hexValue = Integer.toHexString(intValue);
unicodeStringBuilder.append("\\u");
for (int length = hexValue.length(); length < 4; length++) {
unicodeStringBuilder.append("0");
}
unicodeStringBuilder.append(hexValue);
}
return unicodeStringBuilder.toString();
}
Run Code Online (Sandbox Code Playgroud)
这在我的程序之外工作正常但在我的程序中引起了问题.这发生在线路上char[] str = utf8.toCharArray();
某种程度上我失去了我的日本unicode字符,这发生了,因为t将这些字符分成char数组中的2.
所以我决定byte []改用它.
public static String convertToEscaped(String utf8) throws java.lang.Exception
{
byte str[] = utf8.getBytes();
StringBuilder unicodeStringBuilder = new StringBuilder();
for(int i = 0; i < str.length - 1 ; i+=2){
int intValue = (int) str[i]* 256 + (int)str[i+1];
String hexValue = Integer.toHexString(intValue);
unicodeStringBuilder.append("\\u");
for (int length = hexValue.length(); length < 4; length++) {
unicodeStringBuilder.append("0");
}
unicodeStringBuilder.append(hexValue);
}
return unicodeStringBuilder.toString();
}
Run Code Online (Sandbox Code Playgroud)
输出:\ u3132\u3334\u3536\u3538\u3238\u2383\u2028\uffffe282\uffffa1e3\uffff81b7\uffffe283\uffff82e3\uffff81af\uffffe282\uffffb8e3\uffff82af\uffffe283\uffffbbe3\uffff81ad\uffffe283\uffffb2e3\uffff81b0\u2920
但这也是错误的,因为我将两个单字节字符合并为一个.我该怎么做才能克服这个问题?
我不知道你的其他代码的具体要求.但我的建议是不要重新发明轮子并使用API的内置编码功能.
例如getBytes,使用StandardCharsets.UTF_16BE或StandardCharsets.UTF_16LE基于您需要的字节序调用:
String s = "1234567(Asics (??????????) )";
byte[] utf8 = s.getBytes(StandardCharsets.UTF_8);
byte[] utf16 = s.getBytes(StandardCharsets.UTF_16BE); // high order byte first
System.out.println(s.length()); // 28
System.out.println(utf8.length); // 48
System.out.println(utf16.length); // 56 (2 bytes for each char)
Run Code Online (Sandbox Code Playgroud)