×

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

How to compare dates in Java

Introduction: In Java, dates can be compared using a similar interface's compareTo() technique. This method returns 'zero' if each date is the same, returns a rate "more than 0" if...

6 minutes read.

Java String vs StringBuffer

Java String vs StringBuffer In this section, we will discuss the key differences between String and StringBuffer class. Before moving to the ahead in this section, let’s introduce with both classes. String...

4 minutes read.

Java Snippets

The term "snippet" refers to a section of code that addresses numerous issues with just a few lines of code. Decreases the number of lines of code and improves programmer...

3 minutes read.

if-else Program in Java

if-else Program in Java The if-else program in Java controls which code snippet will execute. The if-else program is very basic and yet very important. Because it checks how well one...

19 minutes read.

Java Math negateExact() Method

The negateExact() method of Math class returns the negation for the specified argument, throwing an exception if the result overflows an int or a long. Syntax: public static int negateExact (int a)public...

1 minute read.

Spliterator in Java 8

In this tutorial, we will understand the meaning of spliterator in java 8. It is just like any other iterator available in java used to traverse the elements of either...

4 minutes read.

Java Class Keyword

We know that java is object-oriented programming language which contains the essential key concepts such as classes and objects etc to have clear idea about the object-oriented programming.Java is mainly...

3 minutes read.

Access modifiers in Java

Access modifiers in Java with Example In Java, there are two types of access modifiers one is a non-access modifier, and other is access modifier. If we talk about access modifier, there...

3 minutes read.

House Numbers in Java

In this section, we will discuss about house number in Java. It is a sum of cubes, each of which has a dimension of h + 1. There is a...

3 minutes read.

This Operator Using in Java

this keyword in Java has a wide range of applications. this reference variable in Programming language refers to the object of interest. This keyword is used in Java. this keyword is...

5 minutes read.

Minimum XOR value pair in Java

In this section, you will discuss about minimum XOR value pair in Java. The objective is to enforce a value that indicates the least XOR values of the two numbers from...

4 minutes read.

Mutable class in Java

A language for object-oriented programming is Java. Because this is an object-oriented language of programming, all of its mechanisms and methods are based on objects. Java has a concept of...

6 minutes read.

Construct the Largest Number from the Given Array in Java

In this section, we will create a Java programme that will enable you to locate the greatest integer in an array. The programme will begin comparing the array's numbers with...

3 minutes read.

URLConnection class in Java

A communication channel between the URL and the program is represented by the Java URLConnection class. It may be utilized to read from and write to the given resource the...

4 minutes read.

Can we override Private Method in Java

In this article we will learn the concept of override, private method in java and we will now whether we can override private method in java or not. Override in Java Any...

3 minutes read.

Java Integer getInteger() method

The getInteger() method of Integer class determines the integer value of the system property with the given name. Syntax` public static Integer getInteger(String nm) Parameters The parameter ‘nm’ represents the property name. Throws The getInteger ()...

1 minute read.

Java gc()

Garbage collection Java language provides different ways to perform the task of memory management. In Java, objects are declared and assigned references. Once lifeof an object is completed it is dereferenced...

4 minutes read.

Check whether Java is installed or not

As we know that there are various operating systems, to check whether Java is installed or not in Windows and Mac we use the following ways.           Windows Operating System: There are several...

2 minutes read.

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

How to Solve the Deprecated Error in Java?

Deprecated Java methods should not be used because they are deprecated (often, there are better, more modern alternatives). API update. Until now, Java has kept everything backward compatible and never...

3 minutes read.