Convert Decimal To Hex

Hey guys!! Welcome to flower brackets blog. Below is one such kind of conversion, that is, convert decimal to hex.

Conversion programs in java are very common. In this post let us learn how to write a java program to convert a decimal number to hexadecimal number.

Moving ahead, in number system decimal number has a base of ten. It has digits from 0 to 9. Whereas hexadecimal number has a base 16.

It has digits from 0 to 9 and A to F. Now let’s see the comparison through a table,

convert decimal to hex

As you can see in the above table, from 0 to 9 the numbers are same in both number system. After 10, 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 the hexadecimal system.

In the previous post we saw how to convert binary to decimal and decimal to binary java program. So, let’s see java program to convert decimal to hexadecimal,

Example : convert decimal to hex

import java.util.Scanner;

public class DecimalToHex 
{	
   static char hexadecimalChar[] = {'F','E','D','C','B','A'};
		
   static String toHexadecimal(int num)
   {
      String output = "";
      int temp;
		
      while(num > 0)
      {
         temp = num % 16;
         if(temp > 9)
         {
            output = hexadecimalChar[15 - temp] + output;
         }
         else
         {
            output = temp + output;
         }
         num = num / 16;
      }
      return output;
   }

   public static void main(String[] args) 
   {	
      Scanner sc = new Scanner(System.in);			
      System.out.println("Please enter decimal number : ");
      int number = sc.nextInt();
      System.out.println("The converted hexadecimal value is : " + toHexadecimal(number));
      sc.close();
   }
}

Output:

Please enter decimal number : 234
The converted hexadecimal value is : EA

 


Java program to convert a decimal number to hexadecimal without using methods

Now we will see program for decimal to hexadecimal conversion without using predefined methods. So, let’s get into it,

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 " + decimalNumber + " is : " + hexaDecimal);      
      sc.close();
   }
}

Output:

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


Decimal to hexadecimal using toHexString() method

Here we will see java program to convert a decimal number to hexadecimal using java tohexstring method.

Also Read – Hexadecimal to decimal java program

Let’s see the code implementation,

import java.util.Scanner;

public class DecimalToHexaExample
{
   public static void main(String[] args)
   {
      Scanner sc = new Scanner(System.in);
      System.out.print("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 : 10
The hexadecimal value is : a


Java program to convert decimal to hexadecimal using recursion

Now we are going to execute a java program to convert decimal to hexadecimal using recursion.

import java.util.Scanner;

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

Output:

Please enter decimal number: 14
The hexadecimal value is : E


Conclusion

That’s it guys. This is all about java program to convert decimal to hexadecimal with an example.

I hope you guys have understood the convert decimal to hex java. You can subscribe to my blog flower brackets if you haven’t already.

Do share this article if you like.

Related Posts