Posted to r/learnpython
The challenge
The challenge was how to convert from base 10 to an arbitrary base (in this case base 2 to base 36) in Python.
To represent the digits beyond 9
we choose to use the uppercase letters A-Z
.
It’s then a simple process
if we take base 16 (Hexadecimal) as the target we can represent a number as:
163+162+161+160
The least significant entry (160) is simply the remainder when we divide the number we are trying to convert by 16, so we can just grab the character associated with that value and add it to the result of the floor division (the //
operator in Python) of the same number, leading to a simple recursive solution…
|
|