get Bytes from CharBuffer - Android java.nio

Android examples for java.nio:CharBuffer

Description

get Bytes from CharBuffer

Demo Code


//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();
    }
}

Related Tutorials