Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def convert_base(num, to_base=10, from_base=10):
- # Сперва совершается перевод в целое число
- if isinstance(num, str):
- n = int(num, from_base)
- else:
- n = int(num)
- # Теперь целое число конвертируется в 'to_base' сс
- alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
- if n < to_base:
- return alphabet[n]
- else:
- return convert_base(n // to_base, to_base) + alphabet[n % to_base]
- print(convert_base(7, 3, 10)) #перевод из десятичной в троичную для проверки работоспособности
Advertisement
Add Comment
Please, Sign In to add comment