beginnersbook.com

  • Home
  • All Tutorials
    • Learn Servlet
    • Learn JSP
    • Learn JSTL
    • Learn C
    • Learn C++
    • Learn MongoDB
    • Learn XML
    • Learn Python
    • Learn Perl
    • Learn Kotlin
    • Learn jQuery
  • Core Java
  • OOPs
  • Collections
  • Java I/O
  • JSON
  • DBMS

Java Hexadecimal to Decimal Conversion with examples

By Chaitanya Singh | Filed Under: Java Conversion

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 ❯

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 Conversion

  • Java String to int
  • Java int to String
  • Java String to long
  • Java long to String
  • Java String to double
  • Java double to String
  • Java double to int
  • Java int to double
  • Java int to long
  • Java long to int
  • Java char to String
  • Java char to int
  • Java int to char
  • Java float to String
  • Java boolean to String
  • Java String to boolean
  • Java binary to decimal
  • Java decimal to binary
  • Java binary to Octal
  • Java decimal to hexadecimal
  • Java Hex to decimal
  • Java decimal to Octal
  • Java Octal to decimal
  • Java ASCII to String
  • Java Writer to String
  • Java StackTrace to String
  • Java String to ArrayList
  • Java StringBuffer to String
  • Java InputStream to String

Recently Added..

  • JSON Tutorial
  • Java Regular Expressions Tutorial
  • Java Enum Tutorial
  • Java Annotations Tutorial

Copyright © 2012 – 2022 BeginnersBook . Privacy Policy . Sitemap