从编码的unicode String转换为Java String

时间:2021-10-24 20:16:57

I have a string in json data which looks like this:

我在json数据中有一个字符串,如下所示:

#0023Sat Apr 30 10:46:11 UTC 2016#000a[Interoperability]Interoperability#005c Index=Unknown (R03)#000a[Exif]Shutter#005c Speed#005c Value=1/1999 sec#000a[Exif]Bits#005c Per#005c Sample=8 8 8 bits/component/pixel#000a[Exif]Exposure#005c Bias#005c Value=0 EV#000a[Exif]Sub-Sec#005c Time#005c Original=00#000a

All those #XXXX words are unicode.

所有这些#XXXX单词都是unicode。

How do I convert this into a Java String?

如何将其转换为Java String?

1 个解决方案

#1


3  

Pattern p = Pattern.compile("#([0-9A-Fa-f]{4})");
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    int c = Integer.parseInt(m.group(1), 16);
    m.appendReplacement(sb, String.valueOf((char) c));
}
m.appendTail(sb);
return sb.toString();

This assumes that #XXXX encodes a UTF-16 Unicode code point. Unicode code points actually supercede the 16 bit range of #XXXX.

这假定#XXXX编码UTF-16 Unicode代码点。 Unicode代码点实际上取代了#XXXX的16位范围。

#1


3  

Pattern p = Pattern.compile("#([0-9A-Fa-f]{4})");
Matcher m = p.matcher(s);
StringBuffer sb = new StringBuffer();
while (m.find()) {
    int c = Integer.parseInt(m.group(1), 16);
    m.appendReplacement(sb, String.valueOf((char) c));
}
m.appendTail(sb);
return sb.toString();

This assumes that #XXXX encodes a UTF-16 Unicode code point. Unicode code points actually supercede the 16 bit range of #XXXX.

这假定#XXXX编码UTF-16 Unicode代码点。 Unicode代码点实际上取代了#XXXX的16位范围。