How to convert an integer to a string in any base?

前端 未结 27 3147
清歌不尽
清歌不尽 2020-11-22 02:25

Python allows easy creation of an integer from a string of a given base via

int(str, base). 

I want to perform the inverse: creati

27条回答
  •  花落未央
    2020-11-22 02:52

    def base_conversion(num, base):
        digits = []
        while num > 0:
            num, remainder = divmod(num, base)
            digits.append(remainder)
        return digits[::-1]
    

提交回复
热议问题