Java Hex Calculate toHexString(final long num, final char paddingChar, int min, int max)

Here you can find the source of toHexString(final long num, final char paddingChar, int min, int max)

Description

Internal use.

License

Open Source License

Parameter

Parameter Description
num the long number to convert.
paddingChar the character to use for padding.
min the minimum length of the resulting String.
max the maximum length of the resulting String.

Declaration

private static String toHexString(final long num, final char paddingChar, int min, int max) 

Method Source Code

//package com.java2s;
/*//w  w  w . jav a2s .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>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. toHexString(final byte[] data)
  2. toHexString(final byte[] fieldData)
  3. toHexString(final byte[] raw)
  4. toHexString(final int i)
  5. toHexString(final int value)
  6. toHexString(int b)
  7. toHexString(int b)
  8. toHexString(int bits, int value)
  9. toHexString(int decimal, int stringLength)