Java examples for java.lang:String UTF
Convert to String from UTF Byte Array
//package com.java2s; public class Main { public static void main(String[] argv) { byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 }; System.out.println(fromUTF8ByteArray(bytes)); }//from w w w . j a va 2 s. co m public static String fromUTF8ByteArray(byte[] bytes) { int i = 0; int length = 0; while (i < bytes.length) { length++; if ((bytes[i] & 0xf0) == 0xf0) { // surrogate pair length++; i += 4; } else if ((bytes[i] & 0xe0) == 0xe0) { i += 3; } else if ((bytes[i] & 0xc0) == 0xc0) { i += 2; } else { i += 1; } } char[] cs = new char[length]; i = 0; length = 0; while (i < bytes.length) { char ch; if ((bytes[i] & 0xf0) == 0xf0) { int codePoint = ((bytes[i] & 0x03) << 18) | ((bytes[i + 1] & 0x3F) << 12) | ((bytes[i + 2] & 0x3F) << 6) | (bytes[i + 3] & 0x3F); int U = codePoint - 0x10000; char W1 = (char) (0xD800 | (U >> 10)); char W2 = (char) (0xDC00 | (U & 0x3FF)); cs[length++] = W1; ch = W2; i += 4; } else if ((bytes[i] & 0xe0) == 0xe0) { ch = (char) (((bytes[i] & 0x0f) << 12) | ((bytes[i + 1] & 0x3f) << 6) | (bytes[i + 2] & 0x3f)); i += 3; } else if ((bytes[i] & 0xd0) == 0xd0) { ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f)); i += 2; } else if ((bytes[i] & 0xc0) == 0xc0) { ch = (char) (((bytes[i] & 0x1f) << 6) | (bytes[i + 1] & 0x3f)); i += 2; } else { ch = (char) (bytes[i] & 0xff); i += 1; } cs[length++] = ch; } return new String(cs); } }