Java Long to Hex longToHexString(long num)

Here you can find the source of longToHexString(long num)

Description

Converts the given long value to hex string.

License

Open Source License

Parameter

Parameter Description
num the long type value to convert.

Return

a string representing given long number in hexadecimal format.

Declaration

public static String longToHexString(long num) 

Method Source Code

//package com.java2s;
/*/*from   w w w .jav a  2 s . c o  m*/
 * This file is part of SerialPundit.
 * 
 * Copyright (C) 2014-2016, Rishi Gupta. All rights reserved.
 *
 * The SerialPundit is DUAL LICENSED. It is made available under the terms of the GNU Affero 
 * General Public License (AGPL) v3.0 for non-commercial use and under the terms of a commercial 
 * license for commercial use of this software. 
 * 
 * The SerialPundit 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.
 */

public class Main {
    /**
     * <p>Converts the given long value to hex string.</p>
     * 
     * @param num the long type value to convert.
     * @return a string representing given long number in hexadecimal format.
     */
    public static String longToHexString(long num) {
        return toHexString(num, '0', 16, 16);
    }

    /**
     * <p>Internal use.</p>
     * 
     * @param num the long number to convert.
     * @param paddingChar the character to use for padding.
     * @param min the minimum length of the resulting String.
     * @param max the maximum length of the resulting String.
     */
    private static String toHexString(final long num, final char paddingChar, int min, int max) {
        /* Formats a long number into the specified length hex String. This is identical to Long.toHexString() 
         * except that it pads (with 0's), or truncates, to the specified size. If max < min, the 
         * functionality is exactly as Long.toHexString(). */
        StringBuffer sb = new StringBuffer(Long.toHexString(num));

        if (max < min) {
            return sb.toString();
        }

        while (sb.length() < max) {
            sb.insert(0, paddingChar);
        }

        return sb.substring(0, min);
    }
}

Related

  1. longToHexBytes(long i)
  2. longToHexChars(long value, int length)
  3. longToHexStr(long src, int len, int code)
  4. LongToHexString(final long value)
  5. longToHexString(long n, int digits)
  6. longToHexString(long number)
  7. longtoHexString(long wert, int bits)
  8. longToHexval(long l)