回答編集履歴
1
数値変換案
test
CHANGED
@@ -1,3 +1,79 @@
|
|
1
1
|
難しいも何も、今の`HextoDex`メソッドに4文字つなげたStringを渡せば実現できるかと思うのですが。
|
2
2
|
|
3
3
|
もっとも、変換メソッドはもっと簡略化の余地が有りますが。
|
4
|
+
|
5
|
+
|
6
|
+
|
7
|
+
---
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
コードをちょっと書いてみました
|
12
|
+
|
13
|
+
パターン1:使う文字を文字列として用意しておく
|
14
|
+
|
15
|
+
```java
|
16
|
+
|
17
|
+
class Number{
|
18
|
+
|
19
|
+
public static final String num = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
20
|
+
|
21
|
+
|
22
|
+
|
23
|
+
private static int toDec(char c){
|
24
|
+
|
25
|
+
int n = num.indeOf(c);
|
26
|
+
|
27
|
+
if(n >= 0) return n;
|
28
|
+
|
29
|
+
//その文字が36進数で使う文字でなかった場合に例外を発生させる
|
30
|
+
|
31
|
+
else throw new IndexOutOfBoundsException(String.valueOf(c));
|
32
|
+
|
33
|
+
}
|
34
|
+
|
35
|
+
public static int HextoDec(String s){
|
36
|
+
|
37
|
+
//ここはそのまま
|
38
|
+
|
39
|
+
}
|
40
|
+
|
41
|
+
}
|
42
|
+
|
43
|
+
```
|
44
|
+
|
45
|
+
|
46
|
+
|
47
|
+
パターン2:文字コードで判別
|
48
|
+
|
49
|
+
```java
|
50
|
+
|
51
|
+
class Number{
|
52
|
+
|
53
|
+
private static int toDec(char c){
|
54
|
+
|
55
|
+
if(c >= '0' && c <= '9') return c - '0';
|
56
|
+
|
57
|
+
if(c >= 'A' && c <= 'Z') return 10 + c - 'A';
|
58
|
+
|
59
|
+
//小文字にも対応してみた
|
60
|
+
|
61
|
+
if(c >= 'a' && c <= 'z') return 10 + c - 'a';
|
62
|
+
|
63
|
+
throw new IllegalArgumentException(String.valueOf(c));
|
64
|
+
|
65
|
+
}
|
66
|
+
|
67
|
+
public static int HextoDec(String s){
|
68
|
+
|
69
|
+
//ここはそのまま
|
70
|
+
|
71
|
+
}
|
72
|
+
|
73
|
+
}
|
74
|
+
|
75
|
+
|
76
|
+
|
77
|
+
```
|
78
|
+
|
79
|
+
例外はRuntimeException系で適当に選定しました。
|