Create a byte array from a char array - Android java.lang

Android examples for java.lang:Character

Description

Create a byte array from a char array

Demo Code

public class Main{

    /**//w  w  w. ja v  a 2  s.c  om
     * Create a byte array from a char array
     * 
     * @param chars
     *            the char array
     * @return a byte array
     */
    public static byte[] charArrayToByteArray(char[] chars) {
        byte[] bytes = new byte[chars.length];
        for (int i = 0; i < chars.length; i++) {
            bytes[i] = (byte) chars[i];
        }
        return bytes;
    }

}

Related Tutorials