-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathHex2Dec.java
More file actions
executable file
·41 lines (39 loc) · 1.23 KB
/
Hex2Dec.java
File metadata and controls
executable file
·41 lines (39 loc) · 1.23 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
/* */ package ch06;
/* */
/* */ import java.util.Scanner;
/* */
/* */
/* */ public class Hex2Dec
/* */ {
/* */ public static void main(String[] args) {
/* 9 */ Scanner input = new Scanner(System.in);
/* */
/* */
/* 12 */ System.out.print("Enter a hex number: ");
/* 13 */ String hex = input.nextLine();
/* */
/* 15 */ System.out.println("The decimal value for hex number " + hex + " is " +
/* 16 */ hexToDecimal(hex.toUpperCase()));
/* */ }
/* */
/* */ public static int hexToDecimal(String hex) {
/* 20 */ int decimalValue = 0;
/* 21 */ for (int i = 0; i < hex.length(); i++) {
/* 22 */ char hexChar = hex.charAt(i);
/* 23 */ decimalValue = decimalValue * 16 + hexCharToDecimal(hexChar);
/* */ }
/* */
/* 26 */ return decimalValue;
/* */ }
/* */
/* */ public static int hexCharToDecimal(char ch) {
/* 30 */ if (ch >= 'A' && ch <= 'F') {
/* 31 */ return 10 + ch - 65;
/* */ }
/* 33 */ return ch - 48;
/* */ }
/* */ }
/* Location: /Volumes/TXS.128G/hope useful/practice/2020.jar!/ch06/Hex2Dec.class
* Java compiler version: 8 (52.0)
* JD-Core Version: 1.1.3
*/