Java Program for Hexadecimal to Decimal Conversion Last Updated : 21 Apr, 2025 Comments Improve Suggest changes 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 : 1ABOutput: 427Input : 1AOutput: 26Approach 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 to Convert Hexadecimal to DecimalExample: 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)); } } Output26 Complexity Analysis:Time complexity: O(n), where n is the length of the input hexadecimal string.Auxiliary space: O(1) Comment More infoAdvertise with us M mayur_patil Follow Improve Article Tags : Java Java Programs Java-Conversion-Programs Practice Tags : Java Explore BasicsIntroduction to Java4 min readJava Programming Basics9 min readJava Methods7 min readAccess Modifiers in Java6 min readArrays in Java9 min readJava Strings8 min readRegular Expressions in Java7 min readOOPs & InterfacesClasses and Objects in Java10 min readJava Constructors10 min readJava OOP(Object Oriented Programming) Concepts10 min readJava Packages7 min readJava Interface11 min readCollectionsCollections in Java12 min readCollections Class in Java13 min readCollection Interface in Java6 min readIterator in Java5 min readJava Comparator Interface6 min readException HandlingJava Exception Handling8 min readJava Try Catch Block4 min readJava final, finally and finalize4 min readChained Exceptions in Java3 min readNull Pointer Exception in Java5 min readException Handling with Method Overriding in Java4 min readJava AdvancedJava Multithreading Tutorial3 min readSynchronization in Java10 min readFile Handling in Java5 min readJava Method References9 min readJava 8 Stream Tutorial15+ min readJava Networking15+ min readJDBC Tutorial12 min readJava Memory Management4 min readGarbage Collection in Java6 min readMemory Leaks in Java3 min readPractice JavaJava Interview Questions and Answers15+ min readJava Programs - Java Programming Examples8 min readJava Exercises - Basic to Advanced Java Practice Programs with Solutions7 min readJava Quiz | Level Up Your Java Skills1 min readTop 50 Java Project Ideas For Beginners and Advanced [Update 2025]15+ min read Like