I write python code of a base conversion from any base system into other any base system, so I break down the code into some processes;
- base-B to base-10
- base-10 to base-B
- base-B to base-B'
overview
base conversion (also known as radix conversion) is translating a number expressed in one numerical system into an equivalent value in another numerical system
# these numbers are same meaning
13 # base 10( known as decimal )
1101 # base 2 ( known as binary )
base-B → base-10
Here, B is an integer greater than 1 (for example, 2).
To convert a number written in base-B into base-10, multiplying each digit by
, where
is the positional power starting at 0 from rightmost integer digit.
In python, this conversion is executed by the built-in function int()
base10 = int('1101', 2) # '1101' is written in base-2
print(base10) # 13
base-10 → base-B
First, base-10 → base-2
On the other hand, when you change decimal into the other base-2 system, you repeatedly divide the number by 2 and record the remainders.
13 = 2 * 6 + 1
6 = 2 * 3 + 0
3 = 2 * 1 + 1
1 = 2 * 0 + 1
=> 1101
collecting the remainders from bottom to top
This process can be implemented in Python as follows.
digits = []
decimal_value = 13
while decimal_value > 0:
remainder = decimal_value % 2
digits.append(remainder)
decimal_value //= 2
print(''.join(reverse(digits))) # 1101
Generalizing to base-B
To convert a base-10 into an arbitary base-B system, 2 parts need to be changed.
- Replace divisor 2 into
base_to - Convert remainders greater than or equal to 10 into characters(A,B,C,...) using
_digit_to_char()in the final code.
# Excerpt from the final code
digits = []
while decimal_value > 0:
remainder = decimal_value % self.base_to # 2→other digit number
digis.append(self._digit_to_char(remainder)) #
decimal_value //= self.base_to # 2→other digit number
base-B → base-B'
B and B' are integer > 1.
generalization of the base convertion process, it can be break down into 2 processes;
- conversion base-B into base-10
- conversion base-10 into base-B'
# base_from, base_to are meaning base-B, base-B'
def convert(self) -> str:
# 1. conversion base-B into base-10
decimal_value = int(self.number, self.base_from)
# 2. conversion base-10 into base-B'
if self.base_to == 10:
return str(decimal_value)
else:
return self._decimal_to_base(decimal_value, self.base_to)
complete code for converting from base-B into base-B'
class RadixConverter:
def __init__(self, number: str, base_from: int, base_to: int):
self.number = number
self.base_from = base_from
self.base_to = base_to
def convert(self) -> str:
# Convert the number from base_from to decimal
decimal_value = int(self.number, self.base_from)
# Convert the decimal value to base_to
if self.base_to == 10:
return str(decimal_value)
else:
return self._decimal_to_base(decimal_value, self.base_to)
def _decimal_to_base(self, decimal_value: int, base: int) -> str:
if decimal_value == 0:
return '0'
digits = []
while decimal_value > 0:
remainder = decimal_value % base
digits.append(self._digit_to_char(remainder))
decimal_value //= base
return ''.join(reversed(digits))
def _digit_to_char(self, digit: int) -> str:
if digit < 10:
return str(digit)
else:
return chr(ord('A') + digit - 10)
Top comments (0)