Android examples for java.nio:CharBuffer
get Bytes from CharBuffer
//package com.java2s; import java.nio.ByteBuffer; import java.nio.CharBuffer; import java.nio.charset.Charset; public class Main { public static byte[] getBytes(char[] chars) { Charset cs = Charset.forName("UTF-8"); CharBuffer cb = CharBuffer.allocate(chars.length); cb.put(chars);// w w w . j a va2s . c om cb.flip(); ByteBuffer bb = cs.encode(cb); return bb.array(); } }