Here you can find the source of toHex(final byte[] bytes)
public static String toHex(final byte[] bytes)
//package com.java2s; // and/or modify it under the terms of the GNU General Public License public class Main { private static char[] hexArray = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public static String toHex(final byte[] bytes) { return toHex(bytes, true); }/*w ww . j a va2 s .c om*/ public static String toHex(final byte[] bytes, final boolean addSpace) { if (bytes == null) { return ""; } return toHex(bytes, addSpace, 0, bytes.length); } public static String toHex(final byte[] bytes, final boolean addSpace, final int index, final int count) { if (bytes == null || bytes.length == 0 || count == 0) { return ""; } char[] str = new char[count * 3]; int tmp; int len = 0; for (int pos = 0; pos != count; ++pos) { tmp = bytes[index + pos] & 0xFF; str[len] = hexArray[tmp >>> 4]; ++len; str[len] = hexArray[tmp & 0x0F]; ++len; if (addSpace) { str[len] = ' '; ++len; } } if (addSpace) { --len; } return new String(str, 0, len); } }