×

How to Convert Hexadecimal to Decimal in Java

How to Convert Hexadecimal to Decimal in Java There are two methods to convert Hexadecimal to Decimal:
  • Using parseInt() method
  • Using user-defined logic
Using Integer.parseInt() method It is a static method of theInteger wrapper class. The Integer.parseInt() method converts string to int with given radix. The signature of the method is given below: public static int parseInt(String hexdecnum,int radix) Where hexdecnumis the string that you want to convert. Example In the following example, we have taken a variable hexavalof type String and assigned“E” to it.decimal is a variable of type int which stores the converted value of the variable hexaval.parseInt() is the static method of Integer wrapper class which belongs to the java.lang package. It parses two arguments: first is hexaval which we want to convert into decimal and thesecond is radix i.e. 16. The println statement prints the converted decimal value of “E”.
public class HexaToDecimalExample
{ 
public static void main(String args[])
{ 
String hexaval="E"; 
int decimal=Integer.parseInt(hexval,16); 
System.out.println("The decimal equivalent of E is: "+decimal); 
}
}
Output
The decimal equivalent of E is: 14
Using user defined logic You can also convert Hexadecimal to Decimal by defining your own logic. Example In the following example, we have taken twoStringvariableshexdec and hex and initialize “E3” and “123456789ABCDEF” to it respectively. We have taken an integer variable decimal and assign 0 to it.Here we will use for loop. The hexdec.length() method returns the length of the String i.e. 1.The charAt() method of String class returns the character at ithposition and stores the character into the variable ch.The hex.indexOf()method of String class returns the indexvalue of the characterfrom the specified string. Let’s see how the loop will execute. For first iteration: decimal=0 i=0                                                          //initial value 0<hexdec.length()                          //condition true ch=hexdec.charAt(0)                     //returns ‘E’ in=hex.indexOf(E)                           //returns index value of E i.e. 14 decimal=16*0+14                             //updated value of decimal is 14 i++                                                          //i increment by 1 i.e. the value of i is 1 For second iteration: decimal=14 i=1 1<hexdec.length()                          //condition true ch=hexdec.charAt(1)                     //returns ‘3’ in=hex.indexOf(3)                           //returns index value of 3 i.e. 3 decimal=16*14+3                             //updated value of decimal is 227 i++                                                          //i increment by 1 i.e. the value of i is 2 Forthird iteration: i=2 2<hexdec.length()                          //condition false Hence the third iteration will not execute the loop. The next statement out of the loop will be execute. The println statement prints the converted decimal value of “E3” i.e.227.
class HexaDecimalToDecimal
{
public static void main(String args[])
{
String hexdec = "E3";
String hex= "0123456789ABCDEF";                          
int decimal = 0;
for (int i = 0; i < hexdec.length(); i++)
{
char ch = hexdec.charAt(i);
int  in= hex.indexOf(ch);
decimal = 16*decimal + in;
}
System.out.print("The decimal equivalent of E3 is: "+decimal);
}   
}
Output
The decimal equivalent of E3 is: 227

Related Topics

Java Program to Create Set of Pairs Using HashSet

HashSet in Java: HashSet is an assortment in Java that has a place with java.util bundle. It inside utilises HashMap to store components. It is an execution of the Set connection...

11 minutes read.

How to Run Applet Program in Java

An applet is a new type of program which is put in a webpage. By inserting applet into a webpage dynamic content can be created. Characteristics of an Applet The applet is...

11 minutes read.

Java LinkedList vs ArrayList

LinkedList In LinkedList, each element is a distinct entity containing an information portion and an address component, and the elements are not kept in consecutive locations. Pointers & addresses are used...

3 minutes read.

Java String intern() method

Java String intern() method returns canonical representation for the String Object. syntax: public String intern() Return: It returns a interned String that to be a pool of unique String. Java String intern() Example 1 public class...

2 minutes read.

Find the Frequency of Each Element in the Array in Java

We may count the occurrence of each element in the array of items. Maintaining one array to store the counts of each array element is one strategy for solving this issue....

3 minutes read.

How to Convert String to double in Java

How to Convert String to double in java It is used if we have to perform mathematical operations on the string that contains a double number. When we get data from...

3 minutes read.

Nested Enum in Java

A class that can be defined within another class is called a nested class in Java. You can logically group classes that are used onlyin one place. This makes encapsulation...

3 minutes read.

Why we use static in Java

Many reserved keywords in Java cannot be used as variable names or identifiers. Java uses static keywords frequently because of its effective memory management system. In most cases, you must...

3 minutes read.

Java program to print matrix in Z form

In this article, you will be acknowledged about what is a matrix along with an example. Also, most importantly, you will learn how to print matrix in Z form. What is...

5 minutes read.

Sublime Number in Java

In this tutorial, we will understand what is meant by sublime number in java. From the point of the coding interview, it is one of the important topics. We will we will...

4 minutes read.

How to create an array in Java

An array is a linear data structure stores a collection of fixed number ofelements of similar type. The size of an array is specified when it is created. The array...

14 minutes read.

Sales Tax Problem in Java

To calculate the sales tax on a purchase in Java, you will need to know the following information: The cost of the item being purchased The sales tax rate You can then use...

4 minutes read.

Vector in Java

Java Vector Class The Vector class is a legacy class that implements a growable array of objects. The components that it contains can be accessed using an integer index. The size of...

26 minutes read.

Trimorphic numbers in Java

Wondered what the Trimorphic number is!! In this article, you can learn about what a Trimorphic number is referred to as and how to find whether a number is trimorphic...

3 minutes read.

Hybrid Inheritance in Java

The most crucial OOPs concept in Java is inheritance, which enables the transfer of a class's properties to another class. It describes the Is-A relationship generally. We can create a...

3 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.

Java If Keyword

Definition: The if statement specifies a section of Java code that will run if an if statement's condition is false. The following conditional statements can be used in Java: To provide a block...

3 minutes read.

Java Math cos() Method

The cos() method of Math class returns the trigonometric cosine of the specified angle. Syntax: public static double cos(double a) Parameters: The parameter ‘a’ represents an angle measured in radians. Return Value: The cos() method returns...

1 minute read.

Binary Search Java

Binary search is a search mechanism for key elements from the given List/Array. In Binary search, the search mechanism is followed by dividing the array into parts; hence the search...

3 minutes read.

Objects and Classes in Java

Objects and Classes in Java Classes and objects are the basic concepts of object-oriented programming. It revolves around the real world entity. In Java, the object is a physical and logical...

5 minutes read.