Here you can find the source of toHexString(long value, int digits)
public static String toHexString(long value, int digits)
//package com.java2s; // Licensed under the Apache License, Version 2.0 (the "License"); public class Main { /**/* www . j av a 2 s. c om*/ * Returns a hexadecimal string representing the passed value, padded * as necessary to the specified number of digits. Padding uses zero for * positive numbers, "F" for negative numbers. For most uses, this is * nicer than the unpadded format of <code>Integer.toHexString()</code>. * <p> * Example: <code>toHex(17, 4)</code> returns "0011", while <code> * toHex(-17, 4)</code> returns "FFEF". * <p> * Hex digits are uppercase; call <code>toLowerCase()</code> on the * returned string if you don't like that. * <p> * The returned value can have as many digits as you like -- you're * not limited to the 16 digits that a long can hold. */ public static String toHexString(long value, int digits) { StringBuilder sb = new StringBuilder(digits); while (digits > 0) { int nibble = (int) (value & 0xF); sb.insert(0, "0123456789ABCDEF".charAt(nibble)); value >>= 4; digits--; } return sb.toString(); } }