Skip to content

Commit ad0e95c

Browse files
author
Christian Bender
authored
Merge pull request TheAlgorithms#424 from khairi96/master
Created HexaDecimalToDecimal.java
2 parents 52f7e4e + d5c673a commit ad0e95c

File tree

1 file changed

+42
-0
lines changed

1 file changed

+42
-0
lines changed

Conversions/HexaDecimalToDecimal.java

+42
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package Conversions;
2+
3+
import java.util.Scanner;
4+
5+
public class HexaDecimalToDecimal {
6+
7+
// convert hexadecimal to decimal
8+
public static int getHexaToDec(String hex){
9+
String digits = "012345678910ABCDEFF";
10+
hex = hex.toUpperCase();
11+
int val = 0;
12+
for (int i = 0; i < hex.length(); i++)
13+
{
14+
int d = digits.indexOf(hex.charAt(i));
15+
val = 16*val + d;
16+
}
17+
return val;
18+
}
19+
20+
// Main method gets the hexadecimal input from user and converts it into Decimal output.
21+
22+
public static void main(String args[])
23+
{
24+
String hexa_Input;
25+
int dec_output;
26+
Scanner scan = new Scanner(System.in);
27+
28+
System.out.print("Enter Hexadecimal Number : ");
29+
hexa_Input = scan.nextLine();
30+
31+
// convert hexadecimal to decimal
32+
33+
dec_output = getHexaToDec(hexa_Input);
34+
/*
35+
Pass the string to the getHexaToDec function
36+
and it returns the decimal form in the variable dec_output.
37+
*/
38+
System.out.println("Number in Decimal: "+dec_output);
39+
40+
41+
}
42+
}

0 commit comments

Comments
 (0)