Java Program For Decimal to Hexadecimal Conversion

Last Updated : 18 Aug, 2026

A decimal number is a number represented in the base-10 number system, which uses digits 0–9. The hexadecimal number system is a base-16 number system, which uses digits 0–9 and letters A–F to represent values from 0 to 15.

  • Each decimal number is repeatedly divided by 16.
  • The remainder obtained in each step represents a hexadecimal digit.

Illustration

Input : 10
Output: A

Input : 2545
Output: 9F1

decToHexaDeci

Approach

To convert decimal to hexadecimal:

  • Divide the decimal number by 16.
  • Store the remainder.
  • Replace the number with the quotient.
  • Repeat the process until the quotient becomes 0.
  • Convert remainders 10–15 into A–F.
  • Print the stored remainders in reverse order.
Java
class GFG {

    static void decimalToHexadecimal(int number) {

        if (number == 0) {
            System.out.print("0");
            return;
        }

        char[] hexDigits = "0123456789ABCDEF".toCharArray();
        char[] result = new char[32];

        int i = 0;

        while (number > 0) {
            int remainder = number % 16;
            result[i++] = hexDigits[remainder];
            number /= 16;
        }

        for (int j = i - 1; j >= 0; j--) {
            System.out.print(result[j]);
        }
    }

    public static void main(String[] args) {

        int number = 2545;

        decimalToHexadecimal(number);
    }
}

Output
9F1

Explanation: The decimalToHexadecimal() method repeatedly divides the decimal number by 16 and stores the remainders. Each remainder from 0–9 is used directly, while 10–15 are represented by A–F. The remainders are then printed in reverse order. For 2545, the remainders are obtained as 1, 15, 9, which gives 9F1 when read in reverse order.

Another Method Using Integer.toString()

Java provides the Integer.toString(int, int) method to convert a decimal integer into a string representation of the specified base. By passing 16 as the radix, the decimal number can be converted directly into hexadecimal form.

Syntax

public static String toString(int i, int radix)

Parameter: The method accepts two parameters:

  • a: This is of integer type and refers to the integer value that is to be converted.
  • base: This is also of integer type and refers to the base that is to be used while representing the strings.

Return Value: The method returns a string representation of the specified argument in the specified base.

Java
class GFG {

    public static void main(String[] args) {

        int number = 2545;

        String hexadecimal = Integer.toString(number, 16);

        System.out.println("Hexadecimal equivalent of "
                + number + " is: " + hexadecimal);
    }
}

Output
Hexadecimal equivalent of 2545 is: 9f1

Explanation: The Integer.toString(number, 16) method converts the decimal number 2545 directly into its hexadecimal representation. The returned result uses lowercase letters (a–f) for hexadecimal values from 10–15.

Comment