-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexadecimalToDecimal.java
More file actions
46 lines (46 loc) · 999 Bytes
/
HexadecimalToDecimal.java
File metadata and controls
46 lines (46 loc) · 999 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
package example;
import java.util.Scanner;
public class HexadecimalToDecimal {
public static void main(String args[]){
//promote the string
System.out.print("Enter a Hexadecimal Code : ");
Scanner input = new Scanner(System.in);
String hexNumber = input.next();
long sum = 0;
long hexSixteen = 1;
for(int i = hexNumber.length()-1;i>=0;i--){
char ch = hexNumber.charAt(i);
int dec =hexCharToDec(ch);
sum += hexSixteen*dec;
hexSixteen *= 16 ;
}
System.out.println("Hexadecimal Code : " + hexNumber + " To Convert \nDecimal Code : " + sum);
}
public static int hexCharToDec(char ch){
int dec ;
switch(ch){
case 'A' :
dec = 10;
break;
case 'B' :
dec = 11;
break ;
case 'C' :
dec = 12;
break ;
case 'D' :
dec = 13;
break ;
case 'E' :
dec = 14;
break ;
case 'F' :
dec = 15;
break ;
default :
dec = ch - 48;
break ;
}
return dec;
}
}