-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathAnyBaseToDecimal.java
73 lines (62 loc) · 2.42 KB
/
AnyBaseToDecimal.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
70
71
72
73
package conversions;
import org.junit.Assert;
import org.junit.Test;
public class AnyBaseToDecimal {
@Test
public void anyBaseToDecimalTest() {
Assert.assertEquals("Invalid Number", convertToDecimal("3", 2));
Assert.assertEquals("3", convertToDecimal("11", 2));
Assert.assertEquals("4", convertToDecimal("100", 2));
Assert.assertEquals("5", convertToDecimal("101", 2));
Assert.assertEquals("10", convertToDecimal("1010", 2));
Assert.assertEquals("1024", convertToDecimal("10000000000", 2));
Assert.assertEquals("Invalid Number", convertToDecimal("8", 8));
Assert.assertEquals("Invalid Number", convertToDecimal("9", 8));
Assert.assertEquals("7", convertToDecimal("7", 8));
Assert.assertEquals("8", convertToDecimal("10", 8));
Assert.assertEquals("9", convertToDecimal("11", 8));
Assert.assertEquals("10", convertToDecimal("12", 8));
Assert.assertEquals("1024", convertToDecimal("2000", 8));
Assert.assertEquals("Invalid Number", convertToDecimal("A", 10));
Assert.assertEquals("10", convertToDecimal("10", 10));
Assert.assertEquals("1024", convertToDecimal("1024", 10));
Assert.assertEquals("Invalid Number", convertToDecimal("G", 16));
Assert.assertEquals("16", convertToDecimal("10", 16));
Assert.assertEquals("17", convertToDecimal("11", 16));
Assert.assertEquals("100", convertToDecimal("64", 16));
Assert.assertEquals("225", convertToDecimal("E1", 16));
Assert.assertEquals("1024", convertToDecimal("400", 16));
}
/**
* This method produces a decimal value of any given input number of any base
*
* @param inpNum String of which we need the decimal value and base in integer format
* @return string format of the decimal value
*/
public String convertToDecimal(String inpNum, int base) {
int len = inpNum.length();
int num = 0;
int pow = 1;
for (int i = len - 1; i >= 0; i--) {
if (valOfChar(inpNum.charAt(i)) >= base) {
return "Invalid Number";
}
num += valOfChar(inpNum.charAt(i)) * pow;
pow *= base;
}
return String.valueOf(num);
}
/**
* This method produces integer value of the input character and returns it
*
* @param c Char of which we need the integer value of
* @return integer value of input char
*/
private static int valOfChar(char c) {
if (c >= '0' && c <= '9') {
return (int) c - '0';
} else {
return (int) c - 'A' + 10;
}
}
}