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

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

    You could use baseconv.py from my project: https://github.com/semente/python-baseconv

    Sample usage:

    >>> from baseconv import BaseConverter
    >>> base20 = BaseConverter('0123456789abcdefghij')
    >>> base20.encode(1234)
    '31e'
    >>> base20.decode('31e')
    '1234'
    >>> base20.encode(-1234)
    '-31e'
    >>> base20.decode('-31e')
    '-1234'
    >>> base11 = BaseConverter('0123456789-', sign='$')
    >>> base11.encode('$1234')
    '$-22'
    >>> base11.decode('$-22')
    '$1234'
    

    There is some bultin converters as for example baseconv.base2, baseconv.base16 and baseconv.base64.

提交回复
热议问题