Skip to content
Related Articles

Related Articles

Java Program For Decimal to Hexadecimal Conversion

Improve Article
Save Article
Like Article
  • Last Updated : 25 Apr, 2022

Given a decimal number N, convert N into an equivalent hexadecimal number i.e convert the number with base value 10 to base value 16. The decimal number system uses 10 digits 0-9 and the Hexadecimal number system uses 0-9, A-F to represent any numeric value.


Input : 10
Output: A

Input : 2545
Output: 9F1


  1. Store the remainder when the number is divided by 16 in an array.
  2. Divide the number by 16 now
  3. Repeat the above two steps until the number is not equal to 0.
  4. Print the array in reverse order now.

Below is the implementation of the above approach:


// Java program to convert Decimal Number
// to Hexadecimal Number
// Importing input output classes
// Main class
public class GFG {
    // Method 1
    // To convert decimal to hexadecimal
    static void decTohex(int n)
        // Creating an array to store octal number
        int[] hexNum = new int[100];
        // counter for hexadecimal number array
        int i = 0;
        while (n != 0) {
            // Storing remainder in hexadecimal array
            hexNum[i] = n % 16;
            n = n / 16;
        // Printing hexadecimal number array
        // in the reverse order
        for (int j = i - 1; j >= 0; j--) {
            if (hexNum[j] > 9)
                System.out.print((char)(55 + hexNum[j]));
    // Method 2
    // Main driver method
    public static void main(String[] args)
        // Custom input decimal number
        // to be converted into hexadecimal number
        int n = 2545;
        // Calling the above Method1 over number 'n'
        // to convert this decimal into hexadecimal number

Time Complexity: O(log N)
Auxiliary Space: O(1)

Another method (using a built-in function): The java.lang.Integer.toString(int a, int base) is an inbuilt method in Java that is used to return a string representation of the argument in the base, specified by the second argument base. If the radix/base is smaller than Character.MIN_RADIX or larger than Character.MAX_RADIX, then the base 10 is used. The ASCII characters are used as digits: 0 to 9 and a to z.

public static String toString(int a, int base)

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.


Input: a = 71, base = 2
Output: 1000111

Input: a = 314, base = 16
Output: 13a

The below programs illustrate the Java.lang.Integer.toString(int a, int base) Method:


// To convert Decimal to Hexadecimal Number//
import java.util.*;
// Main driver method
public class Main {
    public static void main(String[] args)
        // Number in decimal
        int number = 2545;
        // output
        System.out.println(Integer.toString(number, 16));
// This code is written by ZEESHAN AHMAD//

Time complexity: O(N)
Auxiliary Space: O(1)

My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!