|  | 
| 1 | 1 | package Conversions; | 
| 2 | 2 | 
 | 
| 3 |  | -import java.io.BufferedReader; | 
| 4 |  | -import java.io.InputStreamReader; | 
| 5 |  | - | 
| 6 | 3 | /** | 
| 7 |  | - * | 
| 8 | 4 |  * @author Varun Upadhyay (https://github.com/varunu28) | 
| 9 |  | - * | 
| 10 | 5 |  */ | 
| 11 | 6 | 
 | 
| 12 | 7 | // Driver program | 
| 13 | 8 | public class AnyBaseToDecimal { | 
| 14 |  | -    public static void main (String[] args) throws Exception{ | 
| 15 |  | -        BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); | 
| 16 |  | -         | 
| 17 |  | -        String inp = br.readLine(); | 
| 18 |  | -        int base =  Integer.parseInt(br.readLine()); | 
| 19 |  | - | 
| 20 |  | -        System.out.println("Input in base " + base + " is: " + inp); | 
| 21 |  | -        System.out.println("Decimal value of " + inp + " is: " + convertToDecimal(inp, base)); | 
| 22 |  | - | 
| 23 |  | -        br.close(); | 
|  | 9 | +    public static void main(String[] args) { | 
|  | 10 | +        assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2); | 
|  | 11 | +        assert convertToDecimal("777", 8) == Integer.valueOf("777", 8); | 
|  | 12 | +        assert convertToDecimal("999", 10) == Integer.valueOf("999", 10); | 
|  | 13 | +        assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16); | 
|  | 14 | +        assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36); | 
| 24 | 15 |     } | 
| 25 | 16 | 
 | 
| 26 | 17 |     /** | 
| 27 |  | -     * This method produces a decimal value of any given input number of any base | 
| 28 |  | -     * @param inp_num String of which we need the decimal value and base in integer format | 
| 29 |  | -     * @return string format of the decimal value | 
|  | 18 | +     * Convert any radix to decimal number | 
|  | 19 | +     * | 
|  | 20 | +     * @param s     the string to be convert | 
|  | 21 | +     * @param radix the radix | 
|  | 22 | +     * @return decimal of bits | 
|  | 23 | +     * @throws NumberFormatException if {@code bits} or {@code radix} is invalid | 
| 30 | 24 |      */ | 
| 31 |  | - | 
| 32 |  | -    public static String convertToDecimal(String inp_num, int base) { | 
| 33 |  | -        int len = inp_num.length(); | 
|  | 25 | +    public static int convertToDecimal(String s, int radix) { | 
| 34 | 26 |         int num = 0; | 
| 35 | 27 |         int pow = 1; | 
| 36 | 28 | 
 | 
| 37 |  | -        for (int i=len-1; i>=0; i--) { | 
| 38 |  | -            if (valOfChar(inp_num.charAt(i)) >= base) { | 
| 39 |  | -                return "Invalid Number"; | 
|  | 29 | +        for (int i = s.length() - 1; i >= 0; i--) { | 
|  | 30 | +            int digit = valOfChar(s.charAt(i)); | 
|  | 31 | +            if (digit >= radix) { | 
|  | 32 | +                throw new NumberFormatException("For input string " + s); | 
| 40 | 33 |             } | 
| 41 |  | -            num += valOfChar(inp_num.charAt(i))*pow; | 
| 42 |  | -            pow *= base; | 
|  | 34 | +            num += valOfChar(s.charAt(i)) * pow; | 
|  | 35 | +            pow *= radix; | 
| 43 | 36 |         } | 
| 44 |  | -        return String.valueOf(num); | 
|  | 37 | +        return num; | 
| 45 | 38 |     } | 
| 46 | 39 | 
 | 
| 47 | 40 |     /** | 
| 48 |  | -     * This method produces integer value of the input character and returns it | 
| 49 |  | -     * @param c Char of which we need the integer value of | 
| 50 |  | -     * @return integer value of input char | 
|  | 41 | +     * Convert character to integer | 
|  | 42 | +     * | 
|  | 43 | +     * @param c the character | 
|  | 44 | +     * @return represented digit of given character | 
|  | 45 | +     * @throws NumberFormatException if {@code ch} is not UpperCase or Digit character. | 
| 51 | 46 |      */ | 
| 52 |  | - | 
| 53 | 47 |     public static int valOfChar(char c) { | 
| 54 |  | -        if (c >= '0' && c <= '9') { | 
| 55 |  | -            return (int)c - '0'; | 
| 56 |  | -        } | 
| 57 |  | -        else { | 
| 58 |  | -            return (int)c - 'A' + 10; | 
|  | 48 | +        if (!(Character.isUpperCase(c) || Character.isDigit(c))) { | 
|  | 49 | +            throw new NumberFormatException("invalid character :" + c); | 
| 59 | 50 |         } | 
|  | 51 | +        return Character.isDigit(c) ? c - '0' : c - 'A' + 10; | 
| 60 | 52 |     } | 
| 61 | 53 | } | 
0 commit comments