Advertisement
DulcetAirman

UTF-16 to Latin-1

Apr 14th, 2014
127
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1.  
  2.   public static void main(final String[] args) {
  3.     final byte[] utf16 = "hello wörld".getBytes(Charset.forName("UTF-16"));
  4.     final byte[] latin1 = utf16toLatin1(utf16);
  5.     System.out.println(new String(latin1, Charset.forName("ISO-8859-1")));
  6.   }
  7.  
  8.   /** Extracts Latin1 text from UTF16 by ignoring every other byte. */
  9.   static byte[] utf16toLatin1(final byte[] data) throws IllegalArgumentException {
  10.     final byte[] result = new byte[data.length / 2];
  11.     int order = 0;// Big-Endian
  12.     int start = 0;// Without BOM
  13.     if (data[0] == -2 && data[1] == -1)
  14.       start = 2;// With BOM
  15.     else if (data[0] == -1 && data[1] == -2) {
  16.       order = 1;// Little-Endian
  17.       start = 2;// With BOM
  18.     } else if (data[0] != 0 && data[1] == 0)
  19.       order = 1;// Little-Endian
  20.  
  21.     for (int i = start; i < data.length; i++) {
  22.       final byte b = data[i];
  23.       if ((i & 1) == order) {
  24.         if (b != 0)
  25.           throw new IllegalArgumentException();
  26.         continue;
  27.       }
  28.       result[i / 2] = data[i];
  29.     }
  30.     return result;
  31.   }
Advertisement
Advertisement
Advertisement
RAW Paste Data Copied
Advertisement