A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/java/java-program-for-hexadecimal-to-decimal-conversion/ below:

Java Program for Hexadecimal to Decimal Conversion

Java Program for Hexadecimal to Decimal Conversion

Last Updated : 21 Apr, 2025

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:

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));
    }
}

Complexity Analysis:



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4