Here you can find the source of chars2Bytes(char[] chars)
public static byte[] chars2Bytes(char[] chars)
//package com.java2s; //License from project: Creative Commons License import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; public class Main { public static byte[] chars2Bytes(char[] chars) { if (null == chars) { return null; }//from ww w. java 2s . c om Charset cs = Charset.forName("UTF-8"); CharBuffer cb = CharBuffer.allocate(chars.length); cb.put(chars); cb.flip(); ByteBuffer bb = cs.encode(cb); return bb.array(); } }