Java Program to Convert Hexadecimal to Decimal

« Previous Program Next Program »

Convert Hexadecimal to Decimal

To convert hexadecimal to decimal in Java Programming, you have to ask to the user to enter any number in hexadecimal number format to convert it into decimal number format display the equivalent value in decimal number system as shown in the following program.

Java Programming Code to Convert Hexadecimal to Decimal

Following Java program ask to the user to enter any number in hexadecimal to convert it into decimal, then display the result on the screen:

/* Java Program Example - Convert Hexadecimal to Decimal */
import java.util.Scanner;

public class JavaProgram
    public static int hex2decimal(String s)
             String digits = "0123456789ABCDEF";
             s = s.toUpperCase();
             int val = 0;
             for (int i = 0; i < s.length(); i++)
                 char c = s.charAt(i);
                 int d = digits.indexOf(c);
                 val = 16*val + d;
             return val;
    public static void main(String args[])
        String hexdecnum;
        int decnum;
        Scanner scan = new Scanner(;
        System.out.print("Enter Hexadecimal Number : ");
        hexdecnum = scan.nextLine();
        decnum = hex2decimal(hexdecnum);
        System.out.print("Equivalent Decimal Number is " + decnum);

When the above Java Program is compile and executed, it will produce the following output:

Java Program convert hexadecimal to decimal

Same Program in Other Languages

You may also like to learn and practice the same program in other popular programming languages:

Java Online Test

« Previous Program Next Program »

© Copyright 2021. All Rights Reserved.