Converts a byte array to a char array. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts a byte array to a char array.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        byte[] pBytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays
                .toString(convertBytesToChars(pBytes)));
    }/*  ww w  . j  ava  2 s  .co  m*/

    /** Converts a <I>byte</I> array to a <I>char</I> array. Simple cast conversion.
       @param pBytes An array of primitive bytes.
       @return An array of primitive chars.
     */
    public static char[] convertBytesToChars(byte[] pBytes) {
        if ((null == pBytes) || (0 == pBytes.length))
            return null;

        char[] pChars = new char[pBytes.length];

        for (int i = 0; i < pBytes.length; i++)
            pChars[i] = (char) (pBytes[i] + 128);

        return pChars;
    }
}

Related Tutorials