-
Notifications
You must be signed in to change notification settings - Fork 0
/
Converter.java
69 lines (69 loc) · 2.19 KB
/
Converter.java
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
public class Converter
{
public static void test()
{
for(int radix = 2;radix <= 36;radix++)
{
System.out.println("[0-"+(radix>10? ("9a-"+(char)(radix+86)):(radix-1))+"]*");
}
System.out.println("" + "aaaa".matches("[a-a]*") + "aa-aa".matches("[a-a]*"));
}
public static void main(String[] args)
{
try
{
System.out.println(convert(args[0],toDecimal(args[1],10),toDecimal(args[2],10)));
}
catch(IllegalArgumentException e)
{
System.out.println("syntax error");
}
}
// Returns a decimal integer equal to the value of a String representing a base 'r' integer
public static int toDecimal(String number,int radix) throws IllegalArgumentException
{
if(radix < 2 || 36 < radix || !number.toLowerCase().matches("[0-"+(radix>10? ("9a-"+(char)(radix+86)):(radix-1))+"]*"))
throw new IllegalArgumentException();
int dec = 0;
int pow = 1;
for(int i = number.length()-1;i >= 0;i--)
{
int c = number.charAt(i);
dec += (c - 48 - (c > 64 ? 7 : 0) - (c > 96 ? 32 : 0))*pow;
pow *= radix;
}
return dec;
}
// Returns a String representing a base 'r' integer whose value is equal to 'dec'
public static String fromDecimal(int decimal,int radix) throws IllegalArgumentException
{
if(radix < 1 || 36 < radix)
throw new IllegalArgumentException();
String s = "";
int pow = 1;
while(pow*radix < decimal)
{
pow *= radix;
}
while(pow >= 1)
{
int val = decimal/pow;
s += (char)(val + (val > 9 ? 55 : 48));
decimal -= val*pow;
pow /= radix;
}
return s;
}
// Uses toDecimal and fromDecimal to convert a String from base r1 to r2
public static String convert(String number,int initialRadix,int finalRadix) throws IllegalArgumentException
{
try
{
return fromDecimal(toDecimal(number,initialRadix),finalRadix);
}
catch(IllegalArgumentException e)
{
throw e;
}
}
}