public static byte[] toUTF8(String s){
  byte[] result=new byte[utfLength(s)];
  int result_index=0;
  for (int i=0, n=s.length(); i < n; ++i) {
    char c=s.charAt(i);
    if (((!WRITE_PSEUDO_UTF8) || (c >= 0x0001)) && (c <= 0x007F)) {
      result[result_index++]=(byte)c;
    }
 else     if (c > 0x07FF) {
      result[result_index++]=(byte)(0xe0 | (byte)(c >> 12));
      result[result_index++]=(byte)(0x80 | ((c & 0xfc0) >> 6));
      result[result_index++]=(byte)(0x80 | (c & 0x3f));
    }
 else {
      result[result_index++]=(byte)(0xc0 | (byte)(c >> 6));
      result[result_index++]=(byte)(0x80 | (c & 0x3f));
    }
  }
  return result;
}

public static byte [ ] toUTF8 ( String s ) { byte [ ] result = new byte [ utfLength ( s ) ] ; int result_index = 0 ; for ( int i = 0 , n = s . length ( ) ; i < n ; ++ i ) { char c = s . charAt ( i ) ; if ( ( ( ! WRITE_PSEUDO_UTF8 ) || ( c >= 0x0001 ) ) && ( c <= 0x007F ) ) { result [ result_index ++ ] = ( byte ) c ; } else if ( c > 0x07FF ) { result [ result_index ++ ] = ( byte ) ( 0xe0 | ( byte ) ( c > > 12 ) ) ; result [ result_index ++ ] = ( byte ) ( 0x80 | ( ( c & 0xfc0 ) > > 6 ) ) ; result [ result_index ++ ] = ( byte ) ( 0x80 | ( c & 0x3f ) ) ; } else { result [ result_index ++ ] = ( byte ) ( 0xc0 | ( byte ) ( c > > 6 ) ) ; result [ result_index ++ ] = ( byte ) ( 0x80 | ( c & 0x3f ) ) ; } } return result ; }
convert the given string into a sequence of ( pseudo - ) utf8 formatted bytes . < p > the output format is controlled by the write_pseudo_utf8 flag .
