Here you can find the source of toBytes(char[] string)
public static byte[] toBytes(char[] string)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; import java.util.Arrays; public class Main { public static byte[] toBytes(char[] string) { CharBuffer charBuffer = CharBuffer.wrap(string); ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer); byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit()); Arrays.fill(charBuffer.array(), '\u0000'); Arrays.fill(byteBuffer.array(), (byte) 0); return bytes; }//from ww w. j a va2s . c om }