-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHexToDecimal.java
More file actions
64 lines (53 loc) · 2.16 KB
/
HexToDecimal.java
File metadata and controls
64 lines (53 loc) · 2.16 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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
/*
* Programmer: Dan Hopp
* Date: 29-SEP-2019
* Description: Prompt the user to enter a hexadecimal number of exactly two
* hex characters. Compute and display the value in decimal. Check for invalid
* input characters and invalid input length (Only valid is 2)
*/
package lab4;
import java.util.Scanner;
public class Problem2 {
public static void main(String[] arg) {
//Declare variables
int charValue1 = 0;
int charValue2 = 0;
//Prompt user for input
Scanner input = new Scanner(System.in);
System.out.print("Enter a two character hex number: ");
String hexString = input.nextLine();
//Validate if string is only 2 in length
if (hexString.length() != 2) {
System.out.println("Please enter exactly two hex characters.");
System.exit(0);
}
//Check for invalid hex characters on 1st char. Adjust its ASCII value
//to the actual numerical value
char ch1 = Character.toUpperCase(hexString.charAt(0));
if (ch1 >= 'A' && ch1 <= 'F') {
charValue1 = ch1 - 'A' + 10;
} else if (Character.isDigit(ch1)) {
charValue1 = ch1 - '0';
} else {
System.out.println(ch1 + " is an invalid hex character.");
System.exit(0);
}
//Check for invalid hex characters on the 2nd char. Adjust its ASCII
//value to the actual numerical value
char ch2 = Character.toUpperCase(hexString.charAt(1));
if (ch2 >= 'A' && ch2 <= 'F') {
charValue2 = ch2 - 'A' + 10;
} else if (Character.isDigit(ch2)) {
charValue2 = ch2 - '0';
} else {
System.out.println(ch2 + " is an invalid hex character.");
System.exit(0);
}
//Add the two numerical values and adjust 1st character's value to its
//16^1 number place
int decimalNumber = (charValue1 * 16) + charValue2;
//Display the results
System.out.println("The decimal equivalent of the hex value " + ch1
+ ch2 + " is " + decimalNumber);
}
}