Here you can find the source of bytesToHex(byte b)
public static String bytesToHex(byte b)
//package com.java2s; //License from project: Open Source License public class Main { /** Most significant bits mask for binary operations */ public static final int MOST_SIGNIFICANT_MASK = 0xFF; /** Least significant bits mask for binary operations */ public static final int LEAST_SIGNIFICANT_MASK = 0x0F; /** {@code char[]} in which each position contains the hex value textual representation */ protected static final char[] hexArray = "0123456789ABCDEF" .toCharArray(); public static String bytesToHex(byte b) { return bytesToHex(new byte[] { b }); }/*from ww w . ja va2 s . com*/ /** * Converts each byte nibble to the equivalent hex representation, in a char * @param bytes The bytes * @return A {@link String} containing the hex representation of each nibble */ public static String bytesToHex(byte[] bytes) { char[] hexChars = new char[bytes.length * 2]; for (int j = 0; j < bytes.length; j++) { int v = bytes[j] & MOST_SIGNIFICANT_MASK; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & LEAST_SIGNIFICANT_MASK]; } return new String(hexChars); } }