Convert decimal to hex in java

Let’s learn how do you convert decimal to hex in java?

Convert decimal to hex in java

To convert decimal to hex is one of the common interview question. In number system, decimal number has a base of ten and has digits from 0 to 9.

Whereas hexadecimal number has a base 16 and has digits from 0 to 9 and A to F. Now let’s see the comparison through a table.

java convert decimal to hex

As you can see in the above table, from 0 to 9 numbers are same in both number system.

After number 9, 10 is denoted as ‘A’, 11 is denoted as ‘B’, 12 is denoted as ‘C’, 13 is denoted as ‘D’, 14 is denoted as ‘E’, 15 is denoted as ‘F’ in hexadecimal system. Here’s java program to convert decimal to hex.

import java.util.Scanner;
public class DecimalToHexaExample 
{
   public static void main(String[] args) 
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter decimal number: ");
      int decimal = sc.nextInt();
      String strHexadecimal = "";
      while(decimal != 0)
      {
         int hexNumber = decimal % 16;
         char charHex;
         if(hexNumber <= 9 && hexNumber >= 0)
         {
            charHex = (char)(hexNumber + '0');
         }
         else
         {
            charHex = (char)(hexNumber - 10 + 'A');
         }
         strHexadecimal = charHex + strHexadecimal;
         decimal = decimal / 16;
      }
      System.out.println("Hexadecimal number: " + strHexadecimal);
      sc.close();
   }
}

Output:

Please enter decimal number: 14
Hexadecimal number: E


Let’s learn two more ways to convert decimal to hex.

  • Using Integer.toHexString() method – this method returns string representation of the unsigned integer value represented by the argument in hexadecimal (base 16).
  • Without using predefined method.

Let’s convert decimal number to hexadecimal number using Integer.toHexString() method.

import java.util.Scanner;
public class DecimalToHexaExample
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.println("Please enter decimal number: ");
      int decimalNumber = sc.nextInt();
      String str = Integer.toHexString(decimalNumber);
      System.out.println("The hexadecimal value is: " + str);
      sc.close();
   }
}

Output:

Please enter decimal number: 12
The hexadecimal value is: c


Decimal to hex – without using predefined method

Let’s see decimal to hex conversion in java without using predefined method.

import java.util.Scanner;
public class DecimalToHexadecimal
{
   public static void main(String[] args)
   {
      int temp, decimalNumber;
      String hexaDecimal = "";
      char[] hexa = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
      Scanner sc = new Scanner(System.in);
      System.out.print("Please enter decimal number: ");
      decimalNumber = sc.nextInt();
      while(decimalNumber > 0)
      {
         temp = decimalNumber % 16;
         hexaDecimal = hexa[temp] + hexaDecimal;
         decimalNumber = decimalNumber / 16;
      }
      System.out.print("The hexadecimal value of is: " + hexaDecimal);
      sc.close();
   }
}

Output:

Please enter decimal number: 10
The hexadecimal value is: A


Also read – variables in java