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

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

    Here is an example of how to convert a number of any base to another base.

    from collections import namedtuple
    
    Test = namedtuple("Test", ["n", "from_base", "to_base", "expected"])
    
    
    def convert(n: int, from_base: int, to_base: int) -> int:
        digits = []
        while n:
            (n, r) = divmod(n, to_base)
            digits.append(r)    
        return sum(from_base ** i * v for i, v in enumerate(digits))
    
    
    if __name__ == "__main__":
        tests = [
            Test(32, 16, 10, 50),
            Test(32, 20, 10, 62),
            Test(1010, 2, 10, 10),
            Test(8, 10, 8, 10),
            Test(150, 100, 1000, 150),
            Test(1500, 100, 10, 1050000),
        ]
    
        for test in tests:
            result = convert(*test[:-1])
            assert result == test.expected, f"{test=}, {result=}"
        print("PASSED!!!")
    

提交回复
热议问题