Java Hex Calculate toHexString(long value, int digits)

Here you can find the source of toHexString(long value, int digits)

Description

Returns a hexadecimal string representing the passed value, padded as necessary to the specified number of digits.

License

Apache License

Declaration

public static String toHexString(long value, int digits) 

Method Source Code

//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();
    }
}

Related

  1. toHexString(long i)
  2. toHexString(long l)
  3. toHexString(long l)
  4. toHexString(long l)
  5. toHexString(long l)
  6. toHexString(long value, int digits)
  7. ToHexString(long[] data)
  8. toHexString(String data)
  9. toHexString(String input)