Do a simple conversion of an array of 8 bit characters into a string. - Java java.lang

Java examples for java.lang:char Array

Description

Do a simple conversion of an array of 8 bit characters into a string.

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        byte[] bytes = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(java.util.Arrays.toString(asCharArray(bytes)));
    }//from www  .  j av  a 2  s. c  o m

    /**
     * Do a simple conversion of an array of 8 bit characters into a string.
     *
     * @param bytes 8 bit characters.
     * @return resulting String.
     */
    public static char[] asCharArray(byte[] bytes) {
        char[] chars = new char[bytes.length];

        for (int i = 0; i != chars.length; i++) {
            chars[i] = (char) (bytes[i] & 0xff);
        }

        return chars;
    }
}

Related Tutorials