Java Char Array to Byte Array charArrayToBytes(char[] chars)

Here you can find the source of charArrayToBytes(char[] chars)

Description

char Array To Bytes

License

Open Source License

Declaration

public static byte[] charArrayToBytes(char[] chars) 

Method Source Code

//package com.java2s;

public class Main {
    public static byte[] charArrayToBytes(char[] chars) {
        return charArrayToBytes(chars, chars.length);
    }//from   ww  w  . j  av a  2  s  .c  o  m

    public static byte[] charArrayToBytes(char[] chars, int length) {

        byte[] bytes = new byte[length * 2];

        for (int i = 0, j = 0; j < length; i += 2, j++) {
            int c = chars[j];

            bytes[i] = (byte) (c >> 8);
            bytes[i + 1] = (byte) c;
        }

        return bytes;
    }
}

Related

  1. char2byte(char[] chars, int offset, int len, byte[] result, int roffset)
  2. charArrayToByteArray(char charBuf[])
  3. charArrayToByteArray(char[] c)
  4. charArrayToByteArray(char[] value)
  5. charArrayToByteArray(final char[] message)
  6. chars2Bytes(char[] chars)
  7. chars2Bytes(char[] chars)
  8. charsToBytes(char[] chars)
  9. charsToBytes(char[] chars)