BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

Java Hexadecimal to Decimal Conversion with examples

Last Updated: December 1, 2024 by Chaitanya Singh | Filed Under: java

In this guide, we will learn how to convert a hexadecimal to a decimal number with the help of examples.

Java Hexadecimal to Decimal Conversion example

We can simply use Integer.parseInt() method and pass the base as 16 to convert the given hexadecimal number to equivalent decimal number.

Here we have given a hexadecimal number hexnum and we are converting it into a decimal number by using Integer.parseInt() method and passing the base as 16.

public class JavaExample{  
   public static void main(String args[]){ 
	//given hexadecimal number
	String hexnum = "6F";
		
	//converting hex to decimal by passing base 16 
	int num = Integer.parseInt(hexnum,16);
		
	System.out.println("Decimal equivalent of given hex number: "+num);
   }
}

Output:
Java hexadecimal to decimal example

Java hex to decimal conversion based on user input

In the above example, we have given a number. If we want we can get the input from user and then we can convert the input hexadecimal number to a decimal number using the same logic that we have used above.

import java.util.Scanner;
public class JavaExample{  
   public static void main(String args[]){ 
	Scanner scanner = new Scanner(System.in);
	System.out.print("Enter any hexadecimal number: ");
	String hexnum = scanner.nextLine();
	scanner.close();
		
	//converting hex to decimal by passing base 16 
	int num = Integer.parseInt(hexnum,16);
		
	System.out.println("Decimal equivalent of given hex number: "+num);
   }
}

Output:
Java hexadecimal to decimal example user input

Java hex to decimal using user defined method

Here we are not using any predefined methods for the conversion, we are writing our own logic to convert a given hex number to a decimal number. We have written our conversion logic in a user defined method hexToDecimal(). This example also uses charAt() and indexOf() methods of String class.

public class JavaExample{    
   public static int hexToDecimal(String hexnum){  
	String hstring = "0123456789ABCDEF";  
	hexnum = hexnum.toUpperCase();  
	int num = 0;  
	for (int i = 0; i < hexnum.length(); i++)  
	{  
		char ch = hexnum.charAt(i);  
		int n = hstring.indexOf(ch);  
		num = 16*num + n;  
	}  
	return num;  
   }  
   public static void main(String args[]){    
	System.out.println("Decimal equivalent of 7A is: "+hexToDecimal("7A"));    
   }
}

Output:
Java hexadecimal to decimal using custom logic

❮ PreviousNext ❯

Top Related Articles:

  1. Java Binary to Octal Conversion with examples
  2. Java Integer byteValue() Method
  3. When to use ArrayList and LinkedList in Java
  4. Java Integer compareUnsigned() Method
  5. TreeMap Iterator example – Java

Tags: Java-Conversion

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Comments

  1. Sayan Rana says

    March 14, 2020 at 6:05 PM

    Sir,a program is given which states that:
    WAP in Java to accept a hexadecimal number and convert it to its decimal equivalent using recursive function .
    Eg:
    Hexadecimal Number:A
    Decimal equivalent:10

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Java Tutorial

Java Introduction

  • Java Index
  • Java Introduction
  • History of Java
  • Features of Java
  • C++ vs Java
  • JDK vs JRE vs JVM
  • JVM - Java Virtual Machine
  • First Java Program
  • Variables
  • Data Types
  • Operators

Java Flow Control

  • Java If-else
  • Java Switch-Case
  • Java For loop
  • Java while loop
  • Java do-while loop
  • Continue statement
  • break statement

Java Arrays

  • Java Arrays

OOPs Concepts

  • OOPs Concepts
  • Constructor
  • Java String
  • Static keyword
  • Inheritance
  • Types of inheritance
  • Aggregation
  • Association
  • Super Keyword
  • Method overloading
  • Method overriding
  • Overloading vs Overriding
  • Polymorphism
  • Types of polymorphism
  • Static and dynamic binding
  • Abstract class and methods
  • Interface
  • Abstract class vs interface
  • Encapsulation
  • Packages
  • Access modifiers
  • Garbage Collection
  • Inner classes
  • Static import
  • Static constructor

Java Exception Handling

  • Exception handling
  • Java try-catch
  • Java throw
  • Java throws
  • Checked and Unchecked Exceptions
  • Jav try catch finally
  • Exception Examples
  • Exception Propagation

Collections Framework

  • Collections in Java
  • Java ArrayList
  • Java LinkedList
  • Java Vector
  • Java HashSet
  • Java LinkedHashSet
  • Java TreeSet
  • Java HashMap
  • Java TreeMap
  • Java LinkedHashMap
  • Java Queue
  • Java PriorityQueue
  • Java Deque
  • Comparable interface
  • Comparator interface
  • Collections Interview Questions

MORE ...

  • Java Scanner Class
  • Java 8 Features
  • Java 9 Features
  • Java Conversion
  • Java Date
  • Java Multithreading
  • Java I/O
  • Java Serialization
  • Java Regex
  • Java AWT
  • Java Swing
  • Java Enum
  • Java Annotations
  • Java main method
  • Java Interview Q

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap