Open In App

Python program to convert any base to decimal by using int() method

Last Updated : 13 Jul, 2020
Improve
Improve
Like Article
Like
Save
Share
Report

Given a number and its base, the task is to convert the given number into its corresponding decimal number. The base of number can be anything like digits between 0 to 9 and A to Z. Where the value of A is 10, value of B is 11, value of C is 12 and so on. 

Examples:  

Input : '1011' 
base = 2 
Output : 11 

Input : '1A' 
base = 16
Output : 26

Input : '12345' 
base = 8
Output : 5349

Approach –

  • Given number in string form and base 
  • Now call the builtin function int(‘number’, base) by passing the two parameters any base number in String form and base of that number and store the value in temp 
  • Print the value temp
     

Python3




# Python program to convert any base
# number to its corresponding decimal
# number
  
# Function to convert any base number
# to its corresponding decimal number
def any_base_to_decimal(number, base):
      
    # calling the builtin function 
    # int(number, base) by passing 
    # two arguments in it number in
    # string form and base and store
    # the output value in temp
    temp = int(number, base)
      
    # printing the corresponding decimal
    # number
    print(temp)
  
# Driver's Code
if __name__ == '__main__' :
    hexadecimal_number = '1A'
    base = 16
    any_base_to_decimal(hexadecimal_number, base)


Output:

26

Previous Article
Next Article

Similar Reads

How to compute natural, base 10, and base 2 logarithm for all elements in a given array using NumPy?
numpy.log( ) function in Python returns natural logarithmic of the input where the natural logarithm of a number is its logarithm to the base of the mathematical constant e, where e is an irrational and transcendental number approximately equal to 2.718281828459. Syntax: numpy.log(arr,out) Parameters: arr : Input Value. Can be scalar and numpy ndim
2 min read
How to fix "ValueError: invalid literal for int() with base 10" in Python
The "ValueError: invalid literal for int() with base 10" is a common error in Python that occurs when you try to convert a string to an integer using the int() function, but the string is not in a valid format that can be interpreted as a base 10 integer. This article will explain the causes of this error and provide methods to fix it. What is "Val
3 min read
String to Int and Int to String in Python
Python defines type conversion functions to directly convert one data type to another. This article is aimed at providing the information about converting a string to int and int to string. Converting a string to an int If we want to convert a number that is represented in the string to int, we have to use the int() function. This function is used
2 min read
What is the meaning of invalid literal for int() with base = ' '?
ValueError is encountered when we pass an inappropriate argument type. Here, we are talking about the ValueError caused by passing an incorrect argument to the int() function. When we pass a string representation of a float or any string representation other than that of int, it gives a ValueError. Example 1 : ValueError with base 10. # ValueError
2 min read
Python program to convert Base 4 system to binary number
Given a base 4 number N, the task is to write a python program to print its binary equivalent. Conversion Table: Examples: Input : N=11002233 Output : 101000010101111 Explanation : From that conversion table we changed 1 to 01, 2 to 10 ,3 to 11 ,0 to 00.Input : N=321321 Output: 111001111001Method 1: Take an empty string say resultstr.We convert the
3 min read
Python program to convert int to exponential
Given a number of int type, the task is to write a Python program to convert it to exponential. Examples: Input: 19 Output: 1.900000e+01 Input: 2002 Output: 2.002000e+03 Input: 110102 Output: 1.101020e+05Approach: We will first declare and initialise an integer numberThen we will use format method to convert the number from integer to exponential t
1 min read
Python program to convert float decimal to Octal number
Python doesn't support any inbuilt function to easily convert floating point decimal numbers to octal representation. Let's do this manually. Approach : To convert a decimal number having fractional part into octal, first convert the integer part into octal form and then fractional part into octal form and finally combine the two results to get the
3 min read
Python program to convert hex string to decimal
In Python, element conversion has been a very useful utility as it offers it in a much simpler way than in other languages. This makes Python a robust language; hence, knowledge of interconversions is always a plus for a programmer. This article discusses the hexadecimal string to a decimal number. Let's discuss certain ways in which this can be pe
5 min read
Python Program to Convert Decimal to Hexadecimal
In this article, we will learn how to convert a decimal value(base 10) to a hexadecimal value (base 16) in Python. Method 1: Using hex() function hex() function is one of the built-in functions in Python3, which is used to convert an integer number into its corresponding hexadecimal form. Syntax : hex(x) Parameters : x - an integer number (int obje
3 min read
Python program to convert decimal to binary number
Given a decimal number as input, the task is to write a Python program to convert the given decimal number into an equivalent binary number.Examples :  Input : 7 Output :111 Input :10 Output :1010 Method #1: Recursive solution DecimalToBinary(num): if num >= 1: DecimalToBinary(num // 2) print num % 2 Below is the implementation of the above recu
3 min read
three90RightbarBannerImg