Risonna

Системы счисления

Oct 5th, 2018
250
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.64 KB | None | 0 0
  1. def convert_base(num, to_base=10, from_base=10):
  2.     # Сперва совершается перевод в целое число
  3.     if isinstance(num, str):
  4.         n = int(num, from_base)
  5.     else:
  6.         n = int(num)
  7.     # Теперь целое число конвертируется в 'to_base' сс
  8.     alphabet = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
  9.     if n < to_base:
  10.         return alphabet[n]
  11.     else:
  12.         return convert_base(n // to_base, to_base) + alphabet[n % to_base]
  13. print(convert_base(7, 3, 10)) #перевод из десятичной в троичную для проверки работоспособности
Advertisement
Add Comment
Please, Sign In to add comment