Here you can find the source of bytesToAsciiMaybe(byte[] data)
public static String bytesToAsciiMaybe(byte[] data)
//package com.java2s; 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 . jav a2s . c om*/ 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; } }