Here you can find the source of toHex(int value, int length)
private static String toHex(int value, int length)
//package com.java2s; //License from project: Apache License public class Main { private static String toHex(int value, int length) { char hexDigits[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; StringBuffer buffer = new StringBuffer(length); int shift = length - 1 << 2; for (int i = -1; ++i < length;) { buffer.append(hexDigits[value >> shift & 0xf]); value <<= 4;/*from ww w. j a v a2 s. c om*/ } return buffer.toString(); } }