-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhextodec.java
More file actions
39 lines (39 loc) · 1.32 KB
/
hextodec.java
File metadata and controls
39 lines (39 loc) · 1.32 KB
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
import java.util.Scanner;
public class hextodec {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
while (true){
System.out.print("Enter a Hex number: ");
String hex = input.nextLine();
int decimalValue = 0,tmp;//tmp=temporary
char hexchar;
for (int i = 0; i < hex.length(); i++) {
hexchar = hex.charAt(i);
System.out.println(hexchar);
//if (hexchar >='A' && hexchar <='F')
// tmp= 10+hexchar-'A';
//else // ch is '0', '1', ..., or '9'
// tmp= hexchar-'0';
tmp=(hexchar >='A' && hexchar <='F')?10+hexchar-'A': hexchar-'0';
decimalValue = decimalValue * 16 + tmp;
}
System.out.println("The decimal value for hex number " + hex + " is " +decimalValue);
if (decimalValue==0) System.exit(-1);
}//while
// System.out.println("The decimal value for hex number " + hex + " is " + hexToDecimal(hex.toUpperCase()));
}
public static int hexToDecimal(String hex) {
int decimalValue = 0;
for (int i = 0; i < hex.length(); i++) {
char hexChar = hex.charAt(i);
decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
}
return decimalValue;
}
public static int hexCharToDecimal(char ch) {
if (ch >='A' && ch <='F')
return 10+ch-'A';
else // ch is '0', '1', ..., or '9'
return ch+'0';
}
}//class