Java Convert via ByteBuffer toBytes(char[] string)

Here you can find the source of toBytes(char[] string)

Description

to Bytes

License

Open Source License

Declaration

public static byte[] toBytes(char[] string) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

public class Main {
    public static byte[] toBytes(char[] string) {
        CharBuffer charBuffer = CharBuffer.wrap(string);
        ByteBuffer byteBuffer = Charset.forName("UTF-8").encode(charBuffer);
        byte[] bytes = Arrays.copyOfRange(byteBuffer.array(), byteBuffer.position(), byteBuffer.limit());
        Arrays.fill(charBuffer.array(), '\u0000');
        Arrays.fill(byteBuffer.array(), (byte) 0);
        return bytes;
    }//from   ww  w.  j  a  va2s  . c  om
}

Related

  1. toByteArrayFromInt(int intValue, boolean shortSize)
  2. toByteArrayFromLong(long longValue)
  3. toBytes(BigDecimal number, int byteLength)
  4. toBytes(char[] ch)
  5. toBytes(char[] chars)
  6. toBytes(final float val)
  7. toBytes(final UUID uuid)
  8. toBytes(InputStream input)
  9. toBytes(int value)