Java Byte Array Create toByteArray(char[] charArray)

Here you can find the source of toByteArray(char[] charArray)

Description

A utility method to convert from chars to bytes

License

Open Source License

Parameter

Parameter Description
charArray char[]

Return

byte[]

Declaration

public static byte[] toByteArray(char[] charArray) 

Method Source Code

//package com.java2s;

public class Main {
    /**//from w  ww . ja va2 s  .co  m
     * A utility method to convert from chars to bytes
     * @param charArray char[]
     * @return byte[]
     */
    public static byte[] toByteArray(char[] charArray) {
        if (charArray == null) {
            return null;
        }

        byte[] result = new byte[charArray.length];
        for (int i = 0; i < charArray.length; i++) {
            result[i] = (byte) charArray[i];
        }

        return result;
    }
}

Related

  1. toByteArray(byte b)
  2. toByteArray(byte byteArray)
  3. toByteArray(byte num)
  4. toByteArray(byte value)
  5. toByteArray(char c)
  6. toByteArray(char[] chars)
  7. toByteArray(char[] chars)
  8. toByteArray(char[] src)
  9. toByteArray(CharSequence hexString)