Here you can find the source of bytesToAsciiMaybe(byte[] data)
public static String bytesToAsciiMaybe(byte[] data)
//package com.java2s; //License from project: Open Source License public class Main { public static int PRINTABLE_ASCII_MIN = 0x20; public static int PRINTABLE_ASCII_MAX = 0x7E; public static String bytesToAsciiMaybe(byte[] data) { return bytesToAsciiMaybe(data, 0, data.length); }// w ww . j av a 2 s .c o m public static String bytesToAsciiMaybe(byte[] data, int offset, int length) { StringBuilder ascii = new StringBuilder(); boolean zeros = false; for (int i = offset; i < offset + length; i++) { int c = data[i] & 0xFF; if (isPrintableAscii(c)) { if (zeros) { return null; } ascii.append((char) c); } else if (c == 0) { zeros = true; } else { return null; } } return ascii.toString(); } public static boolean isPrintableAscii(int c) { return c >= PRINTABLE_ASCII_MIN && c <= PRINTABLE_ASCII_MAX; } }