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

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

    def base(decimal ,base) :
        list = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
        other_base = ""
        while decimal != 0 :
            other_base = list[decimal % base] + other_base
            decimal    = decimal / base
        if other_base == "":
            other_base = "0"
        return other_base
    
    print base(31 ,16)
    

    output:

    "1F"

提交回复
热议问题