Skip to content

Commit 0073605

Browse files
authored
Created HexaDecimalToDecimal.java
added HexaDecimalToDecimal.java on conversions. it converts Hexadecimal input to decimal.
1 parent b082ecc commit 0073605

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

Conversions/HexaDecimalToDecimal.java

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
char c = hex.charAt(i);
15+
int d = digits.indexOf(c);
16+
val = 16*val + d;
17+
}
18+
return val;
19+
}
20+
21+
// Main method gets the hexadecimal input from user and converts it into Decimal output.
22+
23+
public static void main(String args[])
24+
{
25+
String hexa_Input;
26+
int dec_output;
27+
Scanner scan = new Scanner(System.in);
28+
29+
System.out.print("Enter Hexadecimal Number : ");
30+
hexa_Input = scan.nextLine();
31+
32+
// convert hexadecimal to decimal
33+
34+
dec_output = getHexaToDec(hexa_Input);
35+
/*
36+
Pass the string to the getHexaToDec function
37+
and it returns the decimal form in the variable dec_output.
38+
*/
39+
System.out.println("Number in Decimal: "+dec_output);
40+
41+
42+
}
43+
}

0 commit comments

Comments
 (0)