Here you can find the source of toHex(byte[] bytes)
public static String toHex(byte[] bytes)
//package com.java2s; //License from project: Open Source License import java.nio.ByteBuffer; public class Main { /**/*w w w.ja v a2 s . c o m*/ * Returns hex-digit (lower case) string. */ public static String toHex(byte[] bytes) { StringBuffer buf = new StringBuffer(bytes.length * 2); for (int i = 0; i < bytes.length; i++) { int value = (int) bytes[i] & 0xff; if (value < 0x10) { buf.append("0"); } buf.append(Integer.toHexString(value)); } return buf.toString(); } /** * convert a ByteBuffer to a String * ByteBuffer is reset to its original state. */ static public String toString(ByteBuffer value, String charsetName) { String result = null; try { result = new String(toBytes(value), charsetName); } catch (Exception e) { } return result; } static public String toString(int value, int radix, int minChars) { String result = Integer.toString(value, radix); int leadZeroes = minChars - result.length(); if (leadZeroes > 0) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < leadZeroes; i++) { buf.append("0"); } buf.append(result); result = buf.toString(); } return result; } /** * Convert a ByteBuffer to a byte[] * ByteBuffer is reset to its original state. */ static public byte[] toBytes(ByteBuffer value) { byte[] result = null; if (value != null && value.remaining() > 0) { result = new byte[value.remaining()]; value.mark(); value.get(result); value.reset(); } return result; } }