-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathConvertFromBaseToBase.java
More file actions
76 lines (66 loc) · 2.37 KB
/
ConvertFromBaseToBase.java
File metadata and controls
76 lines (66 loc) · 2.37 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
65
66
67
68
69
70
71
72
73
74
75
76
import java.util.Map;
import java.util.HashMap;
public class ConvertFromBaseToBase {
Map<Long, Character> mapItoA = new HashMap<>();
Map<Character, Long> mapAtoI = new HashMap<>();
public ConvertFromBaseToBase() {
for (int i = 0; i < 10; i++)
mapItoA.put((long) i, (char) (i + '0'));
for (int i = 0; i < 26; i++)
mapItoA.put((long) (i + 10), (char) (i + 'A'));
for (int i = 0; i < 10; i++)
mapAtoI.put((char) (i + '0'), (long) i);
for (int i = 0; i < 26; i++)
mapAtoI.put((char) (i + 'A'), (long) (i + 10));
}
public String mapItoAAsString() {
return mapAsString(mapItoA);
}
public String mapAtoIAsString() {
return mapAsString(mapAtoI);
}
public String mapAsString(Map<?, ?> map) {
return new StringBuilder()
.append("[\n")
.append(
map
.keySet()
.stream()
.map(k -> "{" + k + " -> " + map.get(k) + "}")
.reduce(
"",
(str, e) -> str + "\t" + e + ",\n"
)
)
.append("]")
.toString();
}
public Long convertFromAtoI(String str, Integer base) {
Long result = mapAtoI.get(str.charAt(0));
for (int i = 1; i < str.length(); i++) {
result = base * result + mapAtoI.get(str.charAt(i));
}
return result;
}
public String convertFromItoA(Long num, Integer base) {
StringBuilder sb = new StringBuilder();
while (num > 0) {
sb.append(mapItoA.get(num % base));
num = num / base;
}
return sb.reverse().toString();
}
public static void main(String[] args) {
String number = "1111010111111010";
Integer b1 = 2;
Integer b2 = 16;
ConvertFromBaseToBase c = new ConvertFromBaseToBase();
// System.out.println(c.mapItoAAsString());
// System.out.println(c.mapAtoIAsString());
System.out.println(number + " is in base " + b1);
Long num = c.convertFromAtoI(number, b1);
System.out.println(number + " as base 10 = " + num);
String r = c.convertFromItoA(num, b2);
System.out.println(number + " as base " + b2 + " = " + r);
}
}