×

How to Convert Decimal to Hexadecimal in Java

How to Convert Decimal to Hexadecimal in Java The hexadecimal number uses 16 values to represent a number. Numbers from 0 to 9 represented by digits and the numbers from 10 to 15 are represented by alphabets from A to F. There are two methods to convert Hexadecimal to decimal are as follows:
  • Using toHexString() method
  • Using user-defined logic
Using Integer.toHexString() method The toHexString() is a static method of Integer wrapper class.It accepts a single parameter of integer type and returns a string representation of int argument in radix16.  The signature of the method is given below: public static String toHexString(int decimal_number) Where decimal_number is the number which you want to convert. Example In the following example, we have taken two variables str1 and str2 of type String which stores the converted Hexadecimal value. The toHexString() is the method of Integer wrapper class which parses the integer arguments 34 and 75 respectively. The first println statement prints the hexadecimal representation of 34 and the second println statement prints the hexadecimal representation of 75.
public class DecimalToHexaExample
{  
public static void main(String args[])
{  
String str1=Integer.toHexString(34);
String str2=Integer.toHexString(75);
System.out.println(str1);  
System.out.println(str2);  
}
}
Output
22
4b
Using user-defined logic You can convert decimal to hexadecimal in using user-defined logic. Example In the following example, we have taken two variablesdecnum (represent decimal number) and rem (represent remainder). We have initialized 45 todecnum. A String type variable hexdecnum initialized to null and it stores the convertedhexadecimal value.We have defined an array hex[]of type charand initialized hexadecimal valuesfrom 0 to F to it. The while loop has the condition decnnum>0 which will repeatedly perform the operations in the while block until decnumis greater than 0 otherwise terminate the execution.The body of the while loop contains three statements. The first statement finds the remainder on dividing decnum by 16, which is obtained by using a modulo operator. In the second statement, the hex[rem] converts the remainder (rem) into character equivalent and concatenates theremainder(rem) into hexdecnum,and stores the results into the variable hexdecnum. The third statement updates the value ofdecnum, i.e., it divides the value by 16 and takes the quotient. These steps perform the operation in the while block until the conditiondecnum>0 becomes false. When the execution of the while loop is completed the println statement prints the hexadecimalrepresentation of the variable decnum which is stores in hexdecnum. It prints the array in reverse order. Remember:If you write the second statement of while loop like this: hexdecnum=hex[rem]+hexdecnum then the output will be 2D.When you change the sequence of the seconds tatement i.e. hexdecnum=hexdecnum+hex[rem]then the hexadecimal representation of 45 will be D2 which is the wrong output.So remember the sequence while you write this statement. This statement performs concatenation instead of addition. One more thing to remember, if you assign any digitamong0 to 9it gives the same digit as output but in string form.
public class DecimalToHexaExample1
{
public static void main(String args[])
{
intdecnum=45, rem;
String hexdecnum="";
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
while(decnum>0)
{
rem = decnum%16;        
hexdecnum = hex[rem] + hexdecnum;
decnum = decnum/16;
}
System.out.println("Hexadecimal Value of 45 is:"+hexdecnum);
}
}
Output
Equivalent Hexadecimal Value of 0 is: 2D

Related Topics

Stable Marriage Problem in Java

Given N men and N women, the Stable Marriage Problem asks you to match up the men and women in such a way that there are never any two people...

6 minutes read.

Swing Program in Java

Java Swing is part of Java Foundation Classes (JFC). The swing toolkit is used to generate Graphical User Interface (GUI) for programs written in Java. The Java swing API is...

4 minutes read.

Constructor Overloading in Java

In Java, constructors can be overloaded just like methods. The idea of having multiple constructors with various parameter sets so that each function Object()  can carry out a particular task...

3 minutes read.

Magic Square in Java

A square with numbers is referred to as a magic square. The numbers in a magic square of order n are arranged to ensure that the sum of all of...

4 minutes read.

Volatile keyword in Java

Multiple threads can change a variable's value by using the volatile keyword. Making classes thread-safe is another application for it. It indicates that using a method or an instance of...

3 minutes read.

Java Integer signum() method

The signum() method of Java Integer class returns the signum function of the specified int value. Syntax public static int signum (int i)  Parameters The parameter ‘i’ represents the value whose signum is to...

1 minute read.

Java Default Keyword

The Default keyword in java programming language is used as access modifier.If any of the variable or the constructor or the methods or the classes are not assigned with the...

3 minutes read.

Java Byte intValue() method

The intValue()  method of Java Integer class returns an int value for this Integer.  Syntax public int intValue()  Parameters NA  Specified by This method is specified by intValue in class Number  Return Value This method returns the numeric...

1 minute read.

Java Math log() Method

The log() method of Math class returns the natural logarithmic value for the specified double argument. Syntax: public static double log(double a) Parameters: The parameter ‘a’ represents the value. Return Value: The log() method returns the...

1 minute read.

Java String valueOf() method

Java String valueOf() method converts different types of values into String. Such as : int to String, long to String, boolean to String, character to String, float to String, double to...

2 minutes read.

Round Robin Scheduling Program in Java

A CPU scheduling technique is known as Round Robin (RR). Additionally, network schedulers employ it. It was created specifically for a time-sharing system. The temporal slicing scheduling algorithm is another...

4 minutes read.

Client Server Program in Java

Client Server Program in Java The client and server are the two main components of socket programming. The client is a computer/node that request for the service and the server is...

7 minutes read.

What is the ambiguity problem in Java?

The ambiguity problem in Java occurs when a method or constructor is overloaded with two or more methods with the same name but different parameters. This can confuse when multiple...

6 minutes read.

For Loop Program in Java

For Loop Program in Java The for-loop program in Java is written when we want a particular set of statements to be executed repeatedly until the given criteria/ condition is met....

8 minutes read.

Java Read File Line by line

Java provides two options to read files line by line. Each option offers different benefits and has its own set of drawbacks. Following are the ways through which on accomplish...

5 minutes read.

Union in Java

Sets.union() method in Java returns an immutable representation of both the union of two sets. Every element that is present in either backup set is included in the set that...

2 minutes read.

Java vs Scala

Java : Java is an object oriented programming language. It is also known as multi threaded language. It was designed by James gosling in the year 1995. We can also say...

4 minutes read.

Isomorphic String in Java

In this tutorial, we will understand what is meant by isomorphic String in java. We will also see a Java program to find out if the string is isomorphic or...

4 minutes read.

Java AWT

Java AWT Java programming is used to develop different types of applications like window-based applications, web applications, Enterprise applications, or mobile applications. For creating standalone applications, Java AWT API is used....

11 minutes read.

Difference Between Java and PHP

The two most used programming languages are PHP and Java. Both of them have a lot of similarities and distinctions. Let's first grasp each of them individually before examining their...

3 minutes read.