Here you can find the source of toHex(byte[] data, int bytesPerGroup)
public static String toHex(byte[] data, int bytesPerGroup)
//package com.java2s; public class Main { private static final char[] HEX = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; public static String toHex(byte data) { return "" + HEX[(data >> 4) & 0xf] + HEX[data & 0xf]; }/*ww w .ja v a 2s. com*/ public static String toHex(byte[] data) { char[] buf = new char[data.length * 2]; for (int i = 0, j = 0, n = data.length; i < n; i++, j += 2) { buf[j] = HEX[(data[i] >> 4) & 0xf]; buf[j + 1] = HEX[data[i] & 0xf]; } return new String(buf); } public static String toHex(byte[] data, int bytesPerGroup) { StringBuilder sb = new StringBuilder(); for (int i = 0, n = data.length; i < n; i++) { if ((i % bytesPerGroup) == 0 && i > 0) { sb.append(' '); } sb.append(HEX[(data[i] >> 4) & 0xf]); sb.append(HEX[data[i] & 0xf]); } return sb.toString(); } }