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

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

    def baseN(num,b,numerals="0123456789abcdefghijklmnopqrstuvwxyz"):
        return ((num == 0) and numerals[0]) or (baseN(num // b, b, numerals).lstrip(numerals[0]) + numerals[num % b])
    

    ref: http://code.activestate.com/recipes/65212/

    Please be aware that this may lead to

    RuntimeError: maximum recursion depth exceeded in cmp
    

    for very big integers.

提交回复
热议问题