Here you can find the source of encodeUTF8(String text)
public static byte[] encodeUTF8(String text)
//package com.java2s; //License from project: Open Source License import java.io.ByteArrayOutputStream; public class Main { public static byte[] encodeUTF8(String text) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); if (c != 0 && c < '\200') { baos.write(c);/*w w w . j a v a2 s . c om*/ } else if (c == 0 || c >= '\200' && c < '\u0800') { baos.write((byte) (0xc0 | 0x1f & c >> 6)); baos.write((byte) (0x80 | 0x3f & c)); } else { baos.write((byte) (0xe0 | 0xf & c >> 12)); baos.write((byte) (0x80 | 0x3f & c >> 6)); baos.write((byte) (0x80 | 0x3f & c)); } } byte ret[] = baos.toByteArray(); return ret; } }