Converts a byte array into an ascii string. - Java java.lang

Java examples for java.lang:byte Array Convert

Description

Converts a byte array into an ascii string.

Demo Code

//package com.java2s;

public class Main {
    public static void main(String[] argv) {
        byte[] data = new byte[] { 34, 35, 36, 37, 37, 37, 67, 68, 69 };
        System.out.println(byteArrayToAsciiDebugString(data));
    }/*from   w ww . j  a  v a2  s . c o m*/

    /**
     * Converts a byte array into an ascii string. Invalid bytes are not shown.
     */
    public static String byteArrayToAsciiDebugString(byte[] data) {
        String result = "";
        if (data == null) {
            return result;
        }
        for (int i = 0; i < data.length; i++) {
            if (data[i] >= 32 && data[i] < 128) {
                result += (char) (data[i] & 0xFF);
            }
        }
        return result;
    }
}

Related Tutorials