Here you can find the source of toHex(int value, int length)
public static String toHex(int value, int length)
//package com.java2s; /**************************************************************************** This file is part of TIImageTool.//w ww . j ava 2s. 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 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); } }