Java Char Array to Byte Array charArrayToByteArray(char charBuf[])

Here you can find the source of charArrayToByteArray(char charBuf[])

Description

Converts a character array (double byte chars) to a byte array.

License

Open Source License

Return

the converted byte array, or null if the input is null

Declaration

public static byte[] charArrayToByteArray(char charBuf[]) 

Method Source Code

//package com.java2s;
/** ***************************************************************
Util.java/*from   www  .j  a  v a2  s  . com*/
Copyright (C) 2001  Brendon Upson 
http://www.wnc.net.au info@wnc.net.au
    
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
    
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.
    
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
    
 *************************************************************** */

public class Main {
    /**
     * Converts a character array (double byte chars) to a byte array. In terms
     * of internationalisation, this chops characters so should be used with care.
     *
     * @return the converted byte array, or null if the input is null
     */
    public static byte[] charArrayToByteArray(char charBuf[]) {
        if (charBuf == null)
            return null;
        int iLen = charBuf.length;
        byte buf[] = new byte[iLen];
        for (int p = 0; p < iLen; p++)
            buf[p] = (byte) (charBuf[p]);

        return buf;
    }
}

Related

  1. char2byte(char[] chars, int offset, int len, byte[] result, int roffset)
  2. charArrayToByteArray(char[] c)
  3. charArrayToByteArray(char[] value)
  4. charArrayToByteArray(final char[] message)
  5. charArrayToBytes(char[] chars)