Here you can find the source of getBytesUtf8(String str)
public static final byte[] getBytesUtf8(String str)
//package com.java2s; //License from project: Open Source License import java.io.UnsupportedEncodingException; public class Main { private static boolean has_utf8 = false; public static final byte[] getBytesUtf8(String str) { if (has_utf8) { try { return str.getBytes("UTF-8"); } catch (UnsupportedEncodingException usx) { // shouldnt... }//from w w w. j a va 2 s. c om } char[] chars = str.toCharArray(); int vlen = chars.length; for (int i = 0; i < chars.length; i++) { char ch = chars[i]; if (ch >= 0 && ch <= 0x07F) { ; } else if (ch >= 0x080 && ch <= 0x07FF) { vlen++; } else if ((ch >= 0x0800 && ch <= 0x0D7FF) || (ch >= 0x00E000 && ch <= 0x00FFFD)) { vlen += 2; } if (ch >= 0x010000 && ch <= 0x10FFFF) { vlen += 3; } else { /* invalid char, ignore */ vlen--; } } byte[] buf = new byte[vlen]; int j = 0; for (int i = 0; i < chars.length; i++) { char ch = chars[i]; if (ch >= 0 && ch <= 0x07F) { buf[j++] = (byte) (ch & 0x07F); } else if (ch >= 0x080 && ch <= 0x07FF) { buf[j++] = (byte) (0xC0 | ((ch & 0x07C0) >> 6)); buf[j++] = (byte) (0x80 | ((ch & 0x003F))); } else if ((ch >= 0x0800 && ch <= 0x0D7FF) || (ch >= 0x00E000 && ch <= 0x00FFFD)) { buf[j++] = (byte) (0xE0 | ((ch & 0x0F000) >> 12)); buf[j++] = (byte) (0x80 | ((ch & 0x00FC0) >> 6)); buf[j++] = (byte) (0x80 | ((ch & 0x0003F))); } if (ch >= 0x010000 && ch <= 0x10FFFF) { /* non dovrebbero essere usate */ buf[j++] = (byte) (0xE0 | ((ch & 0x1C0000) >> 18)); buf[j++] = (byte) (0x80 | ((ch & 0x03F000) >> 12)); buf[j++] = (byte) (0x80 | ((ch & 0x000FC0) >> 6)); buf[j++] = (byte) (0x80 | ((ch & 0x00003F))); } } return buf; } }