This tutorial contains example source codes written in java dealing on how to convert hexadecimal to decimal. As we all know hex to decimal conversion using manual computation is extremely hard. As well know hexadecimal numbers is of base 16 while decimal is of base 10 which is equivalent to our counting numbers. For reference you might want to take a look on the  Mathematical procedures on how to do hexadecimal to decimal conversion in order for you understand what we are trying to accomplish.

Java Hexadecimal to Decimal Conversion

We would be leveraging the use of static method of parseInt(String input, radix) of Integer class. This method accepts the radix parameter which is of course we are expecting it to be 16 as mentioned above. Basically we have implemented a mechanism to accepts input from the console and then we print the converted value. Moreover we will be validating as well the console input, throwing a ‘Invalid Input’ once the conversion fails. This validation is in place for us to handle the possible exception that can be thrown during parsing.

package com.javatutorialhq.tutorial;

import java.util.Scanner;
/*
 * Sample java source code convert hexadecimal to decimal
 */

public class HexToDecimal {

	public static void main(String[] args) {
		System.out.print("Hexadecimal Input:");
		// read the hexadecimal input from the console
		Scanner s = new Scanner(System.in);
		String inputHex = s.nextLine();
		try{
			// actual conversion of hex to decimal
			Integer outputDecimal = Integer.parseInt(inputHex, 16);
			System.out.println("Decimal Equivalent : "+outputDecimal);
		}
		catch(NumberFormatException ne){
			// Printing a warning message if the input is not a valid hex number
			System.out.println("Invalid Input");
		}
		finally{
			s.close();
		}

	}

}

Sample Output: Java hex to decimal conversion

Hexadecimal Input:A1B
Decimal Equivalent : 2587