Java Integer to Hex intToHexString(int num)

Here you can find the source of intToHexString(int num)

Description

Converts the given integer value to hex string.

License

Open Source License

Parameter

Parameter Description
num the integer type value to convert.

Return

a string representing given int number in hexadecimal format.

Declaration

public static String intToHexString(int num) 

Method Source Code

//package com.java2s;
/*/*  w  w w.  j a  v a  2 s.co  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 integer value to hex string.</p>
     * 
     * @param num the integer type value to convert.
     * @return a string representing given int number in hexadecimal format.
     */
    public static String intToHexString(int num) {
        return toHexString(intToUnsignedLong(num), '0', 8, 8);
    }

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

    /**
     * <p>Converts the given integer value to an unsigned long number. The least significant 4 bytes 
     * (32 bits) of the long number will be identical to the least significant 4 bytes (32 bits) of the 
     * integer number and the most significant 4 bytes (32 bits) of the long number will be zero.</p>
     * 
     * @param data the int type value to convert.
     * @return An unsigned long number representing the given int number.
     */
    public static long intToUnsignedLong(int data) {
        return 0x00000000ffffffff & ((long) data);
    }
}

Related

  1. IntToHexString(final int value)
  2. intToHexString(int _i)
  3. intToHexString(int i)
  4. intToHexString(int i)
  5. intToHexString(int i)
  6. intToHexString(int number)
  7. intToHexString(int val, int width)
  8. intToHexString(int value)
  9. intToHexString(int value, int digits)