|
3 | 3 | /**
|
4 | 4 | * @author Varun Upadhyay (<a href="https://github.com/varunu28">...</a>)
|
5 | 5 | */
|
6 |
| -// Driver program |
7 | 6 | public final class AnyBaseToDecimal {
|
8 |
| - private AnyBaseToDecimal() { |
9 |
| - } |
| 7 | + private static final int CHAR_OFFSET_FOR_DIGIT = '0'; |
| 8 | + private static final int CHAR_OFFSET_FOR_UPPERCASE = 'A' - 10; |
10 | 9 |
|
11 |
| - public static void main(String[] args) { |
12 |
| - assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2); |
13 |
| - assert convertToDecimal("777", 8) == Integer.valueOf("777", 8); |
14 |
| - assert convertToDecimal("999", 10) == Integer.valueOf("999", 10); |
15 |
| - assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16); |
16 |
| - assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36); |
| 10 | + private AnyBaseToDecimal() { |
17 | 11 | }
|
18 | 12 |
|
19 | 13 | /**
|
20 |
| - * Convert any radix to decimal number |
| 14 | + * Convert any radix to a decimal number. |
21 | 15 | *
|
22 |
| - * @param s the string to be convert |
23 |
| - * @param radix the radix |
24 |
| - * @return decimal of bits |
25 |
| - * @throws NumberFormatException if {@code bits} or {@code radix} is invalid |
| 16 | + * @param input the string to be converted |
| 17 | + * @param radix the radix (base) of the input string |
| 18 | + * @return the decimal equivalent of the input string |
| 19 | + * @throws NumberFormatException if the input string or radix is invalid |
26 | 20 | */
|
27 |
| - public static int convertToDecimal(String s, int radix) { |
28 |
| - int num = 0; |
29 |
| - int pow = 1; |
| 21 | + public static int convertToDecimal(String input, int radix) { |
| 22 | + int result = 0; |
| 23 | + int power = 1; |
30 | 24 |
|
31 |
| - for (int i = s.length() - 1; i >= 0; i--) { |
32 |
| - int digit = valOfChar(s.charAt(i)); |
| 25 | + for (int i = input.length() - 1; i >= 0; i--) { |
| 26 | + int digit = valOfChar(input.charAt(i)); |
33 | 27 | if (digit >= radix) {
|
34 |
| - throw new NumberFormatException("For input string " + s); |
| 28 | + throw new NumberFormatException("For input string: " + input); |
35 | 29 | }
|
36 |
| - num += valOfChar(s.charAt(i)) * pow; |
37 |
| - pow *= radix; |
| 30 | + result += digit * power; |
| 31 | + power *= radix; |
38 | 32 | }
|
39 |
| - return num; |
| 33 | + return result; |
40 | 34 | }
|
41 | 35 |
|
42 | 36 | /**
|
43 |
| - * Convert character to integer |
| 37 | + * Convert a character to its integer value. |
44 | 38 | *
|
45 |
| - * @param c the character |
46 |
| - * @return represented digit of given character |
47 |
| - * @throws NumberFormatException if {@code ch} is not UpperCase or Digit |
48 |
| - * character. |
| 39 | + * @param character the character to be converted |
| 40 | + * @return the integer value represented by the character |
| 41 | + * @throws NumberFormatException if the character is not an uppercase letter or a digit |
49 | 42 | */
|
50 |
| - public static int valOfChar(char c) { |
51 |
| - if (!(Character.isUpperCase(c) || Character.isDigit(c))) { |
52 |
| - throw new NumberFormatException("invalid character :" + c); |
| 43 | + private static int valOfChar(char character) { |
| 44 | + if (Character.isDigit(character)) { |
| 45 | + return character - CHAR_OFFSET_FOR_DIGIT; |
| 46 | + } else if (Character.isUpperCase(character)) { |
| 47 | + return character - CHAR_OFFSET_FOR_UPPERCASE; |
| 48 | + } else { |
| 49 | + throw new NumberFormatException("invalid character:" + character); |
53 | 50 | }
|
54 |
| - return Character.isDigit(c) ? c - '0' : c - 'A' + 10; |
55 | 51 | }
|
56 | 52 | }
|
0 commit comments