Here you can find the source of toHexString(byte[] bytes)
public static String toHexString(byte[] bytes)
//package com.java2s; public class Main { public static String toHexString(byte[] bytes) { StringBuffer buffer = new StringBuffer(bytes.length); int startIndex = 0; int column = 0; for (int i = 0; i < bytes.length; i++) { column = i % 16;//from w w w . j av a 2 s .c om switch (column) { case 0: startIndex = i; buffer.append(fixHexString(Integer.toHexString(i), 8)).append(": "); buffer.append(toHex(bytes[i])); buffer.append(" "); break; case 15: buffer.append(toHex(bytes[i])); buffer.append(" "); buffer.append(filterString(bytes, startIndex, column + 1)); buffer.append("\n"); break; default: buffer.append(toHex(bytes[i])); buffer.append(" "); } } if (column != 15) { for (int i = 0; i < (15 - column); i++) { buffer.append(" "); } buffer.append(filterString(bytes, startIndex, column + 1)); buffer.append("\n"); } return buffer.toString(); } private static String fixHexString(String hexStr, int length) { if (hexStr == null || hexStr.length() == 0) { return "00000000h"; } else { StringBuffer buf = new StringBuffer(length); int strLen = hexStr.length(); for (int i = 0; i < length - strLen; i++) { buf.append("0"); } buf.append(hexStr).append("h"); return buf.toString(); } } public static String toHex(byte b) { char[] buf = new char[2]; for (int i = 0; i < 2; i++) { // buf[1 - i] = digits[b & 0xF]; b = (byte) (b >>> 4); } return new String(buf); } public static String filterString(byte[] bytes, int offset, int count) { byte[] buffer = new byte[count]; System.arraycopy(bytes, offset, buffer, 0, count); for (int i = 0; i < count; i++) { if (buffer[i] >= 0x0 && buffer[i] <= 0x1F) { buffer[i] = 0x2e; } } return new String(buffer); } }