Python elegant inverse function of int(string,base)

后端 未结 11 853
谎友^
谎友^ 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:21

    Maybe this shouldn't be an answer, but it could be helpful for some: the built-in format function does convert numbers to string in a few bases:

    >>> format(255, 'b') # base 2
    '11111111'
    >>> format(255, 'd') # base 10
    '255'
    >>> format(255, 'o') # base 8
    '377'
    >>> format(255, 'x') # base 16
    'ff'
    

提交回复
热议问题