JavaAlgorithms/Conversions/HexaDecimalToDecimal.java

40 lines
1001 B
Java
Raw Normal View History

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;
}
2020-10-24 18:23:28 +08:00
return val;
}
2020-10-24 18:23:28 +08:00
// Main method gets the hexadecimal input from user and converts it into Decimal output.
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);
2020-10-24 18:23:28 +08:00
System.out.print("Enter Hexadecimal Number : ");
hexa_Input = scan.nextLine();
2020-10-24 18:23:28 +08:00
// convert hexadecimal to decimal
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();
}
}