Here you can find the source of byteToHex(byte[] b, int size)
public static String byteToHex(byte[] b, int size)
//package com.java2s; public class Main { public static String byteToHex(byte[] b, int size) { return byteToHex(b, size, true); }/* www . ja v a2s.c o m*/ public static String byteToHex(byte[] b, int size, boolean left) { byte[] d = new byte[size]; if (left) { //left size System.arraycopy(b, 0, d, 0, (b.length > size) ? size : b.length); } else { //right size System.arraycopy(b, (b.length > size) ? (b.length - size) : 0, d, 0, (b.length > size) ? size : b.length); } return byteToHex(d); } public static String byteToHex(byte[] b) { return byteToHex(b, true); } public static String byteToHex(byte[] b, boolean format) { StringBuilder toHex = new StringBuilder(); for (int i = 0; (null != b) && (i < b.length); i++) { if (format) { toHex.append(' '); } char hi = Character.forDigit((b[i] >> 4) & 0x0F, 16); char lo = Character.forDigit(b[i] & 0x0F, 16); toHex.append(Character.toUpperCase(hi)); toHex.append(Character.toUpperCase(lo)); if (format && 15 == i % 16) { toHex.append('\r').append('\n'); } } return toHex.toString(); } }