Java Char Array Create toCharArray(byte[] data)

Here you can find the source of toCharArray(byte[] data)

Description

to Char Array

License

Open Source License

Declaration

public static char[] toCharArray(byte[] data) 

Method Source Code

//package com.java2s;
/*/*w w w.  j  av a2 s.  c  o  m*/
 * Copyright (C) 2014 Jesse Caulfield 
 *
 * 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 3 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, see <http://www.gnu.org/licenses/>.
 */

public class Main {
    public static char[] toCharArray(byte[] data) {
        if ((data == null) || (data.length % 2 != 0)) {
            return null;
        }
        char[] chrs = new char[data.length / 2];
        for (int i = 0; i < chrs.length; i++) {
            chrs[i] = toChar(new byte[] { data[(i * 2)], data[(i * 2) + 1], });
        }
        return chrs;
    }

    public static char toChar(byte[] data) {
        if ((data == null) || (data.length != 2)) {
            return 0x0;
        }
        return (char) ((0xff & data[0]) << 8 | (0xff & data[1]));
    }
}

Related

  1. charArrayLiteral(CharSequence cs)
  2. toCharacterArray(String s)
  3. toCharArray(boolean[] array)
  4. toCharArray(byte[] barr)
  5. toCharArray(byte[] byteArray)
  6. toCharArray(byte[] input)
  7. toCharArray(byte[] src)
  8. toCharArray(char... values)
  9. toCharArray(CharSequence chars)