Open In App

Java Program for Hexadecimal to Decimal Conversion

Last Updated : 21 Apr, 2025
Suggest changes
Share
Like Article
Like
Report

Given a Hexadecimal (base 16) number N, and our task is to convert the given Hexadecimal number to a Decimal (base 10). The hexadecimal number uses the alpha-numeric values 0-9 and A- F. And the decimal number uses the numeric values 0-9.

Example:

Input : 1AB
Output: 427

Input : 1A
Output: 26

Approach to Convert Hexadecimal to Decimal

The approach to implementing the conversion is mentioned below:

  • The idea is to extract the digits of a given hexadecimal number starting from the rightmost digit.
  • Keep a variable 'dec_value'. 
  • At the time of extracting digits from the hexadecimal number, multiply the digit with the proper base (Power of 16) and add it to the above variable taken that is 'dec_value'. 
  • In the end, the variable 'dec_value' will store the required decimal number.
Java Program For Hexadecimal to Decimal Conversion


Java Program to Convert Hexadecimal to Decimal

Example:

Java
// Java program to convert Hexadecimal  // to Decimal Number // Importing input output classes import java.io.*; // Main class class Geeks {  // Method  // To convert hexadecimal to decimal  static int hexaToDec(String n)  {  // Storing the length of the  int l = n.length();  // Initializing base value to 1, i.e 16^0  int base = 1;  // Initially declaring and initializing  // decimal value to zero  int dec_val = 0;  // Extracting characters as  // digits from last character  for (int i = l - 1; i >= 0; i--) {  // Condition check  // Case 1  // If character lies in '0'-'9', converting  // it to integral 0-9 by subtracting 48 from  // ASCII value  if (n.charAt(i) >= '0'  && n.charAt(i) <= '9') {  dec_val += (n.charAt(i) - 48) * base;  // Incrementing base by power  base = base * 16;  }  // Case 2  // if case 1 is bypassed  // Now, if character lies in 'A'-'F' ,  // converting it to integral 10 - 15 by  // subtracting 55 from ASCII value  else if (n.charAt(i) >= 'A'  && n.charAt(i) <= 'F') {  dec_val += (n.charAt(i) - 55) * base;  // Incrementing base by power  base = base * 16;  }  }  // Returning the decimal value  return dec_val;  }  // Method 2  // Main driver method  public static void main(String[] args)  {  // Custom input hexadecimal number to be  // converted into decimal number  String n = "1A";  // Calling the above method to convert and  // alongside printing the hexadecimal number  System.out.println(hexaToDec(n));  } } 

Output
26 

Complexity Analysis:

  • Time complexity: O(n), where n is the length of the input hexadecimal string.
  • Auxiliary space: O(1)

Next Article

Similar Reads

Practice Tags :