Any Base To Decimal Algorithm

The Any Base to Decimal Algorithm is a method used to convert numbers from any given base (or radix) to a decimal (base-10) representation. This algorithm is commonly employed in mathematics, computer science, and engineering fields, as it allows for easy manipulation and understanding of numbers in various numeral systems such as binary, octal, or hexadecimal. The key principle of the algorithm is based on the positional notation system, where each digit in a number is assigned a specific value according to its position and the base of the numeral system. By systematically evaluating and summing up the values of each digit, the algorithm can efficiently transform a number from any base to its equivalent decimal representation. To perform the Any Base to Decimal Algorithm, one starts by identifying the digits in the given number and their corresponding position from the right, beginning with the least significant digit (position 0). For each digit, the algorithm multiplies the digit's value by the base raised to the power of its position. The resulting products are then summed up to obtain the final decimal representation of the number. For example, to convert the hexadecimal number "2A3" (base-16) to decimal, one would calculate (2 * 16^2) + (10 * 16^1) + (3 * 16^0) = 512 + 160 + 3 = 675. The Any Base to Decimal Algorithm provides a clear and systematic approach for converting numbers between different numeral systems, making it a valuable tool in various applications across disciplines.
package Conversions;

/**
 * @author Varun Upadhyay (https://github.com/varunu28)
 */

// Driver program
public class AnyBaseToDecimal {
    public static void main(String[] args) {
        assert convertToDecimal("1010", 2) == Integer.valueOf("1010", 2);
        assert convertToDecimal("777", 8) == Integer.valueOf("777", 8);
        assert convertToDecimal("999", 10) == Integer.valueOf("999", 10);
        assert convertToDecimal("ABCDEF", 16) == Integer.valueOf("ABCDEF", 16);
        assert convertToDecimal("XYZ", 36) == Integer.valueOf("XYZ", 36);
    }

    /**
     * Convert any radix to decimal number
     *
     * @param s     the string to be convert
     * @param radix the radix
     * @return decimal of bits
     * @throws NumberFormatException if {@code bits} or {@code radix} is invalid
     */
    public static int convertToDecimal(String s, int radix) {
        int num = 0;
        int pow = 1;

        for (int i = s.length() - 1; i >= 0; i--) {
            int digit = valOfChar(s.charAt(i));
            if (digit >= radix) {
                throw new NumberFormatException("For input string " + s);
            }
            num += valOfChar(s.charAt(i)) * pow;
            pow *= radix;
        }
        return num;
    }

    /**
     * Convert character to integer
     *
     * @param c the character
     * @return represented digit of given character
     * @throws NumberFormatException if {@code ch} is not UpperCase or Digit character.
     */
    public static int valOfChar(char c) {
        if (!(Character.isUpperCase(c) || Character.isDigit(c))) {
            throw new NumberFormatException("invalid character :" + c);
        }
        return Character.isDigit(c) ? c - '0' : c - 'A' + 10;
    }
}

LANGUAGE:

DARK MODE: