Q:

Java Convert Hexadecimal to Decimal

belongs to collection: Java Conversion Programs

0

We can convert hexadecimal to decimal in java using Integer.parseInt() method or custom logic.

Java Hexadecimal to Decimal conversion: Integer.parseInt()

The Integer.parseInt() method converts string to int with given redix. The signature of parseInt() method is given below:

 

public static int parseInt(String s,int redix)  

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

Let's see the simple example of converting hexadecimal to decimal in java.

public class HexToDecimalExample1{  
public static void main(String args[]){  
String hex="a";  
int decimal=Integer.parseInt(hex,16);  
System.out.println(decimal);  
}}  

 

Output:

10

Let's see another example of Integer.parseInt() method.

public class HexToDecimalExample2{  
public static void main(String args[]){  
System.out.println(Integer.parseInt("a",16));  
System.out.println(Integer.parseInt("f",16));  
System.out.println(Integer.parseInt("121",16));  
}}  

 

Output:

10
15
289

Java Hexadecimal to Decimal conversion: Custom Logic

We can convert hexadecimal to decimal in java using custom logic.

public class HexToDecimalExample3{    
public static int getDecimal(String hex){  
    String digits = "0123456789ABCDEF";  
             hex = hex.toUpperCase();  
             int val = 0;  
             for (int i = 0; i < hex.length(); i++)  
             {  
                 char c = hex.charAt(i);  
                 int d = digits.indexOf(c);  
                 val = 16*val + d;  
             }  
             return val;  
}  
public static void main(String args[]){    
System.out.println("Decimal of a is: "+getDecimal("a"));  
System.out.println("Decimal of f is: "+getDecimal("f"));  
System.out.println("Decimal of 121 is: "+getDecimal("121"));  
}}    

 

Output:

Decimal of a is: 10
Decimal of f is: 15
Decimal of 121 is: 289

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Java Convert Decimal to Hexadecimal... >>
<< Java Convert Decimal to Binary...