Here you can find the source of byteToHex(byte[] content, int nLength)
public static String byteToHex(byte[] content, int nLength)
//package com.java2s; /**************************************************************************** This file is part of TIImageTool.//from w w w . j ava 2 s . c o m TIImageTool is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version. TIImageTool is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with TIImageTool. If not, see <http://www.gnu.org/licenses/>. Copyright 2011 Michael Zapf www.mizapf.de ****************************************************************************/ public class Main { public static String byteToHex(byte[] content, int nLength) { StringBuilder sb = new StringBuilder(); int nPos = 0; while (nPos < nLength) { int nByteLength = sb.length(); sb.append(toHex(content[nPos], 2)).append(" "); nPos++; } return sb.toString(); } public static String toHex(int value, int length) { return toHex(value, length, false); } public static String toHex(int value, int length, boolean bUppercase) { String HEX = "0123456789abcdef0123456789ABCDEF"; char[] out = new char[length]; int offset = bUppercase ? 16 : 0; for (int i = 0; i < length; i++) { out[length - i - 1] = HEX.charAt(offset + (value & 0x0f)); value >>= 4; } return new String(out); } }