DulcetAirman icon

Convert UTF16 to UTF32

DulcetAirman | PRO | 03/21/14 09:01:08 AM UTC | 0 ⭐ | 853 👁️ | Never ⏰ | []
Java |

540 B

|

None

|

0 👍

/

0 👎

// Note: Since Java 8 we can just stream code points as integers. This was for Java 7.
 
  public static int[] convertUTF16ToUTF32(final String utf16) {
    final int length = utf16.length();
    final int[] utf32 = new int[utf16.codePointCount(0, length)];
    int index = 0;
    int offset = 0;
    int codepoint;
    while (offset < length) {
      codepoint = utf16.codePointAt(offset);
      utf32[index++] = codepoint;
      offset += Character.charCount(codepoint);
    }
    assert index == utf32.length;
    return utf32;
  }

Comments