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

前端 未结 27 3100
清歌不尽
清歌不尽 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:47

    def baseConverter(x, b):
        s = ""
        d = string.printable.upper()
        while x > 0:
            s += d[x%b]
            x = x / b
        return s[::-1]
    

提交回复
热议问题