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

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

Description

chars Bytes

License

Creative Commons License

Declaration

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

Method Source Code

//package com.java2s;
//License from project: Creative Commons License 

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;

public class Main {
    public static byte[] chars2Bytes(char[] chars) {
        if (null == chars) {
            return null;
        }//from ww w.  java  2s . c  om
        Charset cs = Charset.forName("UTF-8");
        CharBuffer cb = CharBuffer.allocate(chars.length);
        cb.put(chars);
        cb.flip();
        ByteBuffer bb = cs.encode(cb);
        return bb.array();
    }
}

Related

  1. charArrayToByteArray(char[] c)
  2. charArrayToByteArray(char[] value)
  3. charArrayToByteArray(final char[] message)
  4. charArrayToBytes(char[] chars)
  5. chars2Bytes(char[] chars)
  6. charsToBytes(char[] chars)
  7. charsToBytes(char[] chars)
  8. charsToBytes(char[] in)
  9. charsToBytes(final char[] chars, final int charOffset, final int length, final byte[] bytes, final int byteOffset)