Java Hex Calculate toHexString(int value, int numDigits)

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

Description

Returns a base-10 integer as a hexadecimal number in String.

License

Open Source License

Parameter

Parameter Description
value base-10 integer to convert
numDigits number of hex digits to display

Return

String representation of the hex number

Declaration

public static String toHexString(int value, int numDigits) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from   w w w .  ja  v a 2s . c om
     * Returns a base-10 integer as a hexadecimal number in String.
     * @param value  base-10 integer to convert
     * @param numDigits  number of hex digits to display
     * @return  String representation of the hex number
     */
    public static String toHexString(int value, int numDigits) {
        StringBuilder result = new StringBuilder();
        for (int i = (numDigits - 1); i >= 0; i--) {
            String digit = toHexDigit(value, i);
            result.append(digit);
        }
        return result.toString().toUpperCase();
    }

    /**
     * Converts a base-ten integer to a hexadecimal value at a particular digit.
     * @param value  base-10 integer to convert
     * @param digitPosition  designated hex digit position
     * @return  String representation of the hex digit
     */
    public static String toHexDigit(int value, int digitPosition) {
        int digitValue = (value >>> (digitPosition * 4)) & 0xf;

        return Integer.toHexString(digitValue);
    }
}

Related

  1. toHexString(int val)
  2. toHexString(int val, int minLength)
  3. toHexString(int value, int len)
  4. ToHexString(int value, int length)
  5. toHexString(int value, int minLength)
  6. toHexString(int[] data)
  7. toHexString(int[] ia)
  8. toHexString(int[] x, int bitsPerDigit)
  9. toHexString(Integer num)