master-algorithms-py/bit_operations/convert_any_base.py
bt3gl a85ed914d3 👾
2023-07-30 21:40:09 -07:00

22 lines
400 B
Python

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# author: bt3gl
def convert_to_any_base(base: int, num: int) -> str:
if num == 0:
return "0"
n = abs(num)
result = ""
while n:
result += str(n % base)
n //= base
if num < 0:
result += '-'
return result[::-1]