javaunicodeutf-16utf-32

How can I convert UTF-16 to UTF-32 in java?


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");

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?


Solution

  • 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;
        }