In this guide, we will learn how to **convert a hexadecimal to a decimal** number with the help of examples.

## Java Hexadecimal to Decimal Conversion example

We can simply use Integer.parseInt() method and pass the base as 16 to convert the given hexadecimal number to equivalent decimal number.

Here we have given a hexadecimal number `hexnum`

and we are converting it into a decimal number by using `Integer.parseInt()`

method and passing the base as 16.

public class JavaExample{ public static void main(String args[]){ //given hexadecimal number String hexnum = "6F"; //converting hex to decimal by passing base 16 int num = Integer.parseInt(hexnum,16); System.out.println("Decimal equivalent of given hex number: "+num); } }

**Output:**

## Java hex to decimal conversion based on user input

In the above example, we have given a number. If we want we can get the input from user and then we can convert the input hexadecimal number to a decimal number using the same logic that we have used above.

import java.util.Scanner; public class JavaExample{ public static void main(String args[]){ Scanner scanner = new Scanner(System.in); System.out.print("Enter any hexadecimal number: "); String hexnum = scanner.nextLine(); scanner.close(); //converting hex to decimal by passing base 16 int num = Integer.parseInt(hexnum,16); System.out.println("Decimal equivalent of given hex number: "+num); } }

**Output:**

## Java hex to decimal using user defined method

Here we are not using any predefined methods for the conversion, we are writing our own logic to convert a given hex number to a decimal number. We have written our conversion logic in a user defined method hexToDecimal(). This example also uses charAt() and indexOf() methods of String class.

public class JavaExample{ public static int hexToDecimal(String hexnum){ String hstring = "0123456789ABCDEF"; hexnum = hexnum.toUpperCase(); int num = 0; for (int i = 0; i < hexnum.length(); i++) { char ch = hexnum.charAt(i); int n = hstring.indexOf(ch); num = 16*num + n; } return num; } public static void main(String args[]){ System.out.println("Decimal equivalent of 7A is: "+hexToDecimal("7A")); } }

**Output:**

Sayan Rana says

Sir,a program is given which states that:

WAP in Java to accept a hexadecimal number and convert it to its decimal equivalent using recursive function .

Eg:

Hexadecimal Number:A

Decimal equivalent:10