This post deals with the methods to inter-convert decimal to any base system number into any base depending on the user input in Python.
Prerequisites: Basics of python looping constructs
Decimal to Any Base – The Method
The basic algorithm for this conversion is to repeatedly divide [integer division] the given decimal number by the target base value until the decimal number becomes 0. The remainder in each case forms the digits of the number in the new base system, however, in the reverse order.
An Upper Limit
The algorithm, in general, will work for any base, but we need digits/characters to represent that many numbers of a base system. For simplicity, we limit ourselves to base 36 i.e 10 numbers + 26 alphabets. [We can differentiate lower and upper case, but we intend to focus on the algorithm here!]. It will be clear after the implementation that the method has to work for any base.
Decimal to Any Base – Python Implementation
Consider the following program,
def dec_to_base[num,base]: #Maximum base - 36 base_num = "" while num>0: dig = int[num%base] if dig 0]:.