Python elegant inverse function of int(string,base)

后端 未结 11 874
谎友^
谎友^ 2020-11-28 06:01

python allows conversions from string to integer using any base in the range [2,36] using:

int(string,base)

im looking for an elegant inver

11条回答
  •  甜味超标
    2020-11-28 06:22

    Here is a recursive function:

    def encode(nIn, nBase):
       n = nIn // nBase
       s = '0123456789abcdefghijklmnopqrstuvwxyz'[nIn % nBase]
       return encode(n, nBase) + s if n > 0 else s
    
    n = 1577858399
    s = encode(n, 36)
    print(s == 'q3ezbz')
    

提交回复
热议问题