如何在java中将UTF-16转换为UTF-32?
问题描述:
我一直在寻找解决方案,但这个主题似乎并不多。我找到了建议的解决方案:
I have looked for solutions, but there doesn't seem to be much on this topic. I have found solutions that suggest:
String unicodeString = new String("utf8 here");
byte[] bytes = String.getBytes("UTF8");
String converted = new String(bytes,"UTF16");
从utf8转换为utf16,但是,java不处理UTF32,这使得这个解决方案不可行。有没有人知道如何实现这个目标?
for converting to utf16 from utf8, however, java doesn't handle "UTF32", which makes this solution unviable. Does anyone know any other way on how to achieve this?
答
搜索后我得到了这个工作:
after searching I got this to work:
public static String convert16to32(String toConvert){
for (int i = 0; i < toConvert.length(); ) {
int codePoint = Character.codePointAt(toConvert, i);
i += Character.charCount(codePoint);
//System.out.printf("%x%n", codePoint);
String utf32 = String.format("0x%x%n", codePoint);
return utf32;
}
return null;
}