Java tutorial
//package com.java2s; //License from project: Open Source License public class Main { private static String getSafePrintChars(byte[] byteArray) { if (byteArray == null) { // return "" instead? throw new IllegalArgumentException("Argument 'byteArray' cannot be null"); } return getSafePrintChars(byteArray, 0, byteArray.length); } private static String getSafePrintChars(byte[] byteArray, int startPos, int length) { if (byteArray == null) { // return "" instead? throw new IllegalArgumentException("Argument 'byteArray' cannot be null"); } if (byteArray.length < startPos + length) { throw new IllegalArgumentException("startPos(" + startPos + ")+length(" + length + ") > byteArray.length(" + byteArray.length + ")"); } StringBuilder buf = new StringBuilder(); for (int i = startPos; i < length; i++) { if (byteArray[i] >= (byte) 0x20 && byteArray[i] < (byte) 0x7F) { buf.append((char) byteArray[i]); } else { buf.append("."); } } return buf.toString(); } }