Here you can find the source of char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
public static int char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag)
//package com.java2s; public class Main { private static byte[] outputByte = new byte[3]; public static int char2ByteUTF8(String input, int inOff, int inEnd, byte[] output, int outOff, int outEnd, boolean getLengthFlag) { char inputChar; int outputSize; int charOff = inOff; int byteOff = outOff; while (charOff < inEnd) { inputChar = input.charAt(charOff); if (inputChar < 0x80) { outputByte[0] = (byte) inputChar; outputSize = 1;/*from ww w.j a v a 2 s.c om*/ } else if (inputChar < 0x800) { outputByte[0] = (byte) (0xc0 | ((inputChar >> 6) & 0x1f)); outputByte[1] = (byte) (0x80 | (inputChar & 0x3f)); outputSize = 2; } else { outputByte[0] = (byte) (0xe0 | ((inputChar >> 12)) & 0x0f); outputByte[1] = (byte) (0x80 | ((inputChar >> 6) & 0x3f)); outputByte[2] = (byte) (0x80 | (inputChar & 0x3f)); outputSize = 3; } if (getLengthFlag) { byteOff += outputSize; } else { if ((byteOff + outputSize) > outEnd) { return -1; } for (int i = 0; i < outputSize; i++) { output[byteOff++] = outputByte[i]; } } charOff++; } return byteOff - outOff; } }