ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Computer Programming Tutorials

How to convert Decimal number to Hexadecimal number in Java ?.

Updated on September 17, 2014

Introduction

In this tutorial, you will learn how to convert a decimal number to hexadecimal number in Java. In number system a decimal number has a base of 10. It has digits from 0 to 9. A hexadecimal number in number system has a base of 16. It has a digits from 0 to 9 and A to F. Comparison between a decimal number and a hexadecimal number is shown in below table. In order to convert a decimal number to hexadecimal number, you have to perform below steps as -

Step 1 - Divide the decimal number by 16. It results in two important values. One is the remainder and other is quotient.

Step 2 - If remainder is less than 9, take it as a digit between 0 to 9. If remainder is between 10 to 15 than take 10 as A, 11 as B, 12 as C, 13 as D, 14 as E and 15 as F. This remainder is the least significant digit for us.

Step 3 - Perform step 1 again with the quotient calculated in step 1. It again results in two important values. One is the remainder and other is quotient.

Step 4 - Perform step 2 as usual.

Step 5 - Perform all above steps till quotient comes out to be zero.

Step 6 - Hexadecimal number is remainder evaluated in reverse direction.


Lets take an example - say you want to convert decimal number 453 to hexadecimal.

Step 1 - Divide 453 by 16. It results in two values as : remainder = 5 and quotient = 28.

Step 2 - As remainder is 5 which is less than 9. Take least significant digit as 5.

Step 3 - Divide 28 by 16. It results in two values as : remainder = 12 and quotient = 1.

Step 4 - As remainder is between 10 to 15, therefore second last significant digit is C.

Step 5 - Divide 1 by 16. It results in two values as : remainder = 1 and quotient = 0.

Step 6 - As quotient is 0 division process stops and remainder is 1. Therefore, hexadecimal number is remainder evaluated in reverse direction. Here, remainder evaluation was 5 -> C -> 1. So, in our example hexadecimal of 453 is 1C5.


Table : Decimal Vs Hexadecimal

Decimal Number
Hexadecimal Number
0
0
1
1
2
2
3
3
4
4
5
5
6
6
7
7
8
8
9
9
10
A
11
B
12
C
13
D
14
E
15
F

Java video tutorial - How to convert Decimal number to Hexadecimal number in Java.

Java source code - How to convert Decimal number to Hexadecimal number in Java.

// Import necessary Java classes from Java API used in the tutorial. 
// Scanner class is in java.util package. It is been used here to take user
// inputs from console.
import java.util.Scanner;

// Create a Java class such as DecimalToHexadecimal.
// This class contains source code for converting 
// decimal number to hexadecimal.
public class DecimalToHexadecimal {

	// Create a main method with a signature such as 
	// public static void main(String[] args). This method
	// contains all the conversion logic. It is the starting
	// point of execution of any Java program.
	public static void main(String[] args) {

		// Create a Scanner object with the help of System.in instance.
		// System.in instance helps Scanner class in reading user inputs
		// from the console.
		Scanner scanner = new Scanner(System.in);

		// Prompt the user to enter a decimal number whose
		// conversion to hexadecimal number is to be done.
		System.out.print("Enter a decimal number - ");
		
		// Create a variable by name decimal. Using scanner class nextInt
		// method take integer value entered by user and assign it to 
		// decimal varuable.
		int decimal = scanner.nextInt();

		// Create a empty String variable which will hold our hexadecimal number.
		String hexadecimal = "";

		// In order to convert decimal number to hexadecimal number. We will
		// iterate using while loop till decimal is not equal to zero.
		while (decimal != 0) {

			// Create a int variable by name hexValue which will hold remainder
			// of decimal number with 16. In order to convert decimal to hexadecimal 
			// we have to divide decimal number by 16 and hold its remainder.
			int hexValue = decimal % 16; 
			
			// Create a char variable by name hexChar. This will help us in holding 
			// hexadecimal numbers which have characters from A to F.
			char hexChar;		

			// If the hexValue which is the remainder of decimal number with 16,
			// comes out to be less than 9. Than take hexValue appended with '0'
			// character and store it in hexChar variable.
			if (hexValue <= 9 && hexValue >= 0) {
				hexChar =  (char)(hexValue + '0'); 
			}
			else {  
				// Else if hexValue comes out to be greater than 9, than we have to append 
				// character A to it because after 9 instead of 10, 11, 12 it has to be output as
				// A, B and C so on .
				hexChar =  (char)(hexValue - 10 + 'A');
			}
			
			// Assign hexChar value appended with hexadecimal and assign it back to 
			// hexadecimal variable.	For each iteration of while loop we have to take 
			// previous hexadecimal value, append it to new hexChar and assign it back 
			// to hexadecimal.
			hexadecimal = hexChar + hexadecimal;
			
			// Follow the process  with new value of decimal, taking in division of decimal by 16.
			decimal = decimal / 16;
		} 

		// Finally printing the hexadecimal value on the Java console.
		System.out.println("The hexadecimal number is " + hexadecimal);
	}
}

Steps to implement -

Step 1 - Import necessary Java classes from Java API used in the tutorial. Scanner class is in java.util package. It is been used here to take user inputs from console.

Step 2 - Create a Java class such as "DecimalToHexadecimal". This class contains source code for converting decimal number to hexadecimal.

Step 3 - Create a main method with a signature such as public static void main(String[] args). This method contains all the conversion logic. It is the starting point of execution of any Java program.

Step 4 - Create a Scanner object with the help of System.in instance. System.in instance helps Scanner class in reading user inputs from the console.

Step 5 - Prompt the user to enter a decimal number whose conversion to hexadecimal number is to be done.

Step 6 - Create a variable by name decimal. Using scanner class nextInt method take integer value entered by user and assign it to decimal variable.

Step 7 - Create a empty String variable which will hold our hexadecimal number.

Step 8 - In order to convert decimal number to hexadecimal number. We will iterate using while loop till decimal is not equal to zero.

Step 9 - Create a int variable by name hexValue which will hold remainder of decimal number with 16. In order to convert decimal to hexadecimal we have to divide decimal number by 16 and hold its remainder.

Step 10 - Create a char variable by name hexChar. This will help us in holding hexadecimal numbers which have characters from A to F.

Step 11 - If the hexValue which is the remainder of decimal number with 16, comes out to be less than 9. Than take hexValue appended with '0' character and store it in hexChar variable.

Step 12 - Else if hexValue comes out to be greater than 9, than we have to append character A to it because after 9 instead of 10, 11, 12 it has to be output as A, B and C so on .

Step 13 - Assign hexChar value appended with hexadecimal and assign it back to hexadecimal variable. For each iteration of while loop we have to take previous hexadecimal value, append it to new hexChar and assign it back to hexadecimal.

Step 14 - Follow the process with new value of decimal, taking in division of decimal by 16.

Step 15 - Finally printing the hexadecimal value on the Java console.

Java program to convert decimal number to hexadecimal number.
Java program to convert decimal number to hexadecimal number.

Output of the program -

Output of the Java program to convert decimal number to hexadecimal number.
Output of the Java program to convert decimal number to hexadecimal number.
Output of the Java program to convert decimal number to hexadecimal number.
Output of the Java program to convert decimal number to hexadecimal number.
Output of the Java program to convert decimal number to hexadecimal number.
Output of the Java program to convert decimal number to hexadecimal number.

Output of the program - Explanation

On running above Java program, it prompts user to enter value of decimal number whose hexadecimal number is to be calculated. User enters a decimal number and hit enter key. The Java code converts decimal number to hexadecimal number and prints hexadecimal number on the console.

For example -

  1. User enters decimal number as 125. It prints hexadecimal number as 7D.
  2. User enters decimal number as 127. It prints hexadecimal number as 7F.
  3. User enters decimal number as 453. It prints hexadecimal number as 1C5.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.