×

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 Future Example

Future is an interface in the Java language that is a part of  java.util.concurrent package. It serves as a symbol for the output of an asynchronous computation. The interface offers ways to determine whether a computation has finished,  wait for it to finish, and receive its result. Once the task or calculation is finished, it cannot be undone. A Future interface offers ways to determine whether the computation is finished, to wait for it to finish, and to receive the computation's results. When the computation...

3 minutes read.

Get yesterdays date by no of days in Java

In this tutorial, we are going to learn how to get yesterday’s date by the no of days in Java. Using the Calendar class, one can get the current date....

1 minute read.

Tower of Hanoi Program in Java

Tower of Hanoi Program in Java The Tower of Hanoi program in Java is written to solve a mathematical puzzle, called Tower of Hanoi, where we have three poles and n...

4 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

How to get the current date and time in Java

Introduction: In this article, we are going to discover many processes for Getting the existing-day Date and Time in Java. Most programs require timestamping events or showing date/times, among many...

3 minutes read.

Sum of digits in string in java

To find the sum of all digits in a string, you need to traverse through the string one by one character; if the character is an integer, you need to...

2 minutes read.

Kong Java Client

Kong is an Organization Microservice Programming interface gateway. Kong gives an adaptable deliberation layer that safely oversees correspondence among clients and microservices by means of a Programming interface. Otherwise called...

6 minutes read.

Java Thread Lifecycle: States and Stages

Multithreading Multithreading is one of the most important features in object-oriented programming, and this multithreading can be implemented by various object-oriented programming languages like Java, Python, and C++. A multithreaded process...

7 minutes read.

Difference between String Tokenizer and split Method in Java

Introduction Today, let us understand the difference between String Tokenizer and Split Method. First, let us learn about the String Tokenizer and Split method individually and then know about their differences String...

7 minutes read.

Prime Points in Java

The points that divide an integer into two halves containing a prime number are known as prime points. Printing every prime point of a specific number is the task. Let's...

6 minutes read.

Java Break Keyword

Break: The word Break is a keyword or a statement in a java programming language.This Keyword break is used to stop the execution of the loop or switch statements etc.The loop...

3 minutes read.

Use Of Adapter class in Java

An adapter class in Java enables listener interfaces to be implemented by default. The Delegation Event Model is where the idea of listener interfaces first appeared. It is one of...

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.

Java Abstraction

Java Abstraction Abstraction is an advanced feature of Java to make it transparent. The main motive behind the abstraction is to deal with ideas, not with events. Abstraction is a process...

4 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

3 minutes read.

Java Write File

In this post, we'll examine various Java programming methods for writing into files. Since this class is character-oriented due to how it is used in file handling in Java, it...

4 minutes read.

Vectors in Java

Vector Class We may make resizable arrays comparable to the ArrayList class using the Vector class, which implements the List interface. A vector is similar to a dynamic collection that can...

4 minutes read.

Dining Philosophers problem in Java

The Problem of the Dining Philosophers illustrates a concurrency issue involving the distribution of scarce resources among conflicting processes. Problem Statement Imagine a dining table with a circle in the middle and five...

3 minutes read.

Java Integer compareTo() method

The compareTo() method of Integer class compares two Integer objects numerically. Syntax public static int compareTo(int anotherInteger) Parameters The parameter ‘anotherInteger’ represents the Integer to be compared. Specified by This method is specified by compareTo in...

2 minutes read.

Blockchain in Java

Blockchain is a continuously expanding ledger that maintains an immutable, secure, and chronological record of all transactions that have ever occurred. It can be utilized to securely transfer money, assets,...

9 minutes read.