Advertisement
DulcetAirman

Convert UTF16 to UTF32

Mar 21st, 2014
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. // Note: Since Java 8 we can just stream code points as integers. This was for Java 7.
  2.  
  3.   public static int[] convertUTF16ToUTF32(final String utf16) {
  4.     final int length = utf16.length();
  5.     final int[] utf32 = new int[utf16.codePointCount(0, length)];
  6.     int index = 0;
  7.     int offset = 0;
  8.     int codepoint;
  9.     while (offset < length) {
  10.       codepoint = utf16.codePointAt(offset);
  11.       utf32[index++] = codepoint;
  12.       offset += Character.charCount(codepoint);
  13.     }
  14.     assert index == utf32.length;
  15.     return utf32;
  16.   }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement