2018-04-21 00:25:21 +08:00
|
|
|
package Conversions;
|
|
|
|
|
|
|
|
import java.util.Scanner;
|
|
|
|
|
|
|
|
public class HexaDecimalToDecimal {
|
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// convert hexadecimal to decimal
|
|
|
|
public static int getHexaToDec(String hex) {
|
|
|
|
String digits = "0123456789ABCDEF";
|
|
|
|
hex = hex.toUpperCase();
|
|
|
|
int val = 0;
|
|
|
|
for (int i = 0; i < hex.length(); i++) {
|
|
|
|
int d = digits.indexOf(hex.charAt(i));
|
|
|
|
val = 16 * val + d;
|
2018-04-21 00:25:21 +08:00
|
|
|
}
|
2020-10-24 18:23:28 +08:00
|
|
|
return val;
|
|
|
|
}
|
2018-04-21 00:25:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// Main method gets the hexadecimal input from user and converts it into Decimal output.
|
2018-04-21 00:25:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
public static void main(String args[]) {
|
|
|
|
String hexa_Input;
|
|
|
|
int dec_output;
|
|
|
|
Scanner scan = new Scanner(System.in);
|
2018-04-21 00:25:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
System.out.print("Enter Hexadecimal Number : ");
|
|
|
|
hexa_Input = scan.nextLine();
|
2018-04-21 00:25:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
// convert hexadecimal to decimal
|
2018-04-21 00:25:21 +08:00
|
|
|
|
2020-10-24 18:23:28 +08:00
|
|
|
dec_output = getHexaToDec(hexa_Input);
|
|
|
|
/*
|
|
|
|
Pass the string to the getHexaToDec function
|
|
|
|
and it returns the decimal form in the variable dec_output.
|
|
|
|
*/
|
|
|
|
System.out.println("Number in Decimal: " + dec_output);
|
|
|
|
scan.close();
|
|
|
|
}
|
2018-04-21 00:25:21 +08:00
|
|
|
}
|