Java Hex Calculate toHexDigit(int value, int digitPosition)

Here you can find the source of toHexDigit(int value, int digitPosition)

Description

Converts a base-ten integer to a hexadecimal value at a particular digit.

License

Open Source License

Parameter

Parameter Description
value base-10 integer to convert
digitPosition designated hex digit position

Return

String representation of the hex digit

Declaration

public static String toHexDigit(int value, int digitPosition) 

Method Source Code

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

public class Main {
    /**/*from  w ww .  j  a v a 2s.c  om*/
     * 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);
    }

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

Related

  1. toHexDigit(int d)
  2. toHexDigit(int d)
  3. toHexDigit(int h)
  4. toHexDigit(int i)
  5. toHexDigit(int number)
  6. toHexDigits(byte[] bytes)
  7. toHexDigits(int value)
  8. toHexDump(byte[] ab, int cBytesPerLine)
  9. toHexDump(byte[] buffer, int offset, int length, boolean hex, boolean ascii)