Convert from any base to decimal and vice versa - GeeksforGeeks
Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. Value of A is 10, value of B is 11 and so on. Write a function to convert the number to decimal.
Read full article from Convert from any base to decimal and vice versa - GeeksforGeeks
Given a number and its base, convert it to decimal. The base of number can be anything such that all digits can be represented using 0 to 9 and A to Z. Value of A is 10, value of B is 11 and so on. Write a function to convert the number to decimal.
int val(char c){ if (c >= '0' && c <= '9') return (int)c - '0'; else return (int)c - 'A' + 10;}// Function to convert a number from given base 'b'// to decimalint toDeci(char *str, int base){ int len = strlen(str); int power = 1; // Initialize power of base int num = 0; // Initialize result int i; // Decimal equivalent is str[len-1]*1 + // str[len-1]*base + str[len-1]*(base^2) + ... for (i = len - 1; i >= 0; i--) { // A digit in input number must be // less than number's base if (val(str[i]) >= base) { printf("Invalid Number"); return -1; } num += val(str[i]) * power; power = power * base; } return num;}