public static byte[] toBytes(String str, int offset, int length) {
        CharBuffer c = CharBuffer.wrap(str, offset, offset + length);
        ByteBuffer b = UTF_8.encode(c);
        byte[] result = new byte[b.remaining()];
        b.get(result);
        return result;
    }
Utility method as toBytes(String).

@param str
           String to encode
@param offset
           index of first character to encode
@param length
           number of characters to encode
@return substring encoded as UTF-8
vespajlib/src/main/java/com/yahoo/text/Utf8.java