Java tutorial
//package com.java2s; //License from project: Apache License import java.nio.CharBuffer; import java.nio.charset.Charset; public class Main { public static byte[] getBytes(char[] buffer, int offset, int length) { CharBuffer cb = CharBuffer.allocate(length); cb.put(buffer, offset, length); cb.flip(); return Charset.defaultCharset().encode(cb).array(); } }