Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static void main(final String[] args) {
- final byte[] utf16 = "hello wörld".getBytes(Charset.forName("UTF-16"));
- final byte[] latin1 = utf16toLatin1(utf16);
- System.out.println(new String(latin1, Charset.forName("ISO-8859-1")));
- }
- /** Extracts Latin1 text from UTF16 by ignoring every other byte. */
- static byte[] utf16toLatin1(final byte[] data) throws IllegalArgumentException {
- final byte[] result = new byte[data.length / 2];
- int order = 0;// Big-Endian
- int start = 0;// Without BOM
- if (data[0] == -2 && data[1] == -1)
- start = 2;// With BOM
- else if (data[0] == -1 && data[1] == -2) {
- order = 1;// Little-Endian
- start = 2;// With BOM
- } else if (data[0] != 0 && data[1] == 0)
- order = 1;// Little-Endian
- for (int i = start; i < data.length; i++) {
- final byte b = data[i];
- if ((i & 1) == order) {
- if (b != 0)
- throw new IllegalArgumentException();
- continue;
- }
- result[i / 2] = data[i];
- }
- return result;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement