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
Let's see the simple example of converting hexadecimal to decimal in java.
Output:
Let's see another example of Integer.parseInt() method.
Output:
Java Hexadecimal to Decimal conversion: Custom Logic
We can convert hexadecimal to decimal in java using custom logic.
Output: