Skip to content

Instantly share code, notes, and snippets.

@aishenreemo
Last active October 8, 2023 04:18
Show Gist options
  • Select an option

  • Save aishenreemo/cf5a2eacd8a8b955a8c147237cb91c25 to your computer and use it in GitHub Desktop.

Select an option

Save aishenreemo/cf5a2eacd8a8b955a8c147237cb91c25 to your computer and use it in GitHub Desktop.
base-A to base-B converter
import os
DEBUG = os.environ.get("DEBUG_CONVERTER") or False
DIGITS = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# in order to convert a number from base-A to base-B
# we have to convert base-A to base-10 then convert the output to base-B
# works if A and B is greater than 1 and less than len(DIGITS)
def main():
base_from = abs(int(input("Enter base from: ")))
base_to = abs(int(input("Enter base to: ")))
value = input("Enter value: ")
base_10_value = base_n_to_base_10(base_from, value)
base_to_value = base_10_to_base_n(base_to, base_10_value)
print()
print(base_10_value)
print(base_to_value)
def base_n_to_base_10(base, value):
if DEBUG:
print(f"BASE {base} TO BASE 10")
values = str(value).split(".")
int_part = values[0]
output = 0.0
# compute the integer part
for ch in int_part:
prev_value = output
output *= base
output += int(ch, base)
if DEBUG:
print(f"({prev_value} * {base}) + {ch} = {output}")
if DEBUG:
print()
# if the value has decimal part
if len(values) > 1:
frac_part = values[1]
# compute the decimal part
# for each (index, character) in enumerated frac_part
# btw, enumerate([a, b, c]) is just [(0, a), (1, b), (2, c)]
for i, ch in enumerate(frac_part):
prev_value = output
output += int(ch, base) * base**(-i - 1)
if DEBUG:
print(f"{prev_value} + {ch} * {base}^({-i - 1}) -> {output}")
if DEBUG:
print()
return str(output)
def base_10_to_base_n(base, value):
if DEBUG:
print(f"BASE 10 TO BASE {base}")
# divide the value into two parts
output = ""
int_value = int(float(value))
frac_value = float(value) - float(int_value)
# compute the integer part
while int_value > 0:
copy = int_value
remainder = int_value % base
output = DIGITS[remainder] + output
int_value //= base
if DEBUG:
print(f"{copy} % {base} = {remainder} -> {output}")
# if the value has decimal part
if frac_value:
output += "."
# compute the compute the decimal part
for _ in range(10):
copy = frac_value
frac_value *= base
digit = int(frac_value)
output += DIGITS[digit]
frac_value -= digit
if DEBUG:
print(f"{copy} * {base} = {digit} -> {output}")
if DEBUG:
print()
return output
if __name__ == "__main__":
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment