K calculated is converted into binary decimal

Question: will be converted to decimal k-ary

Analysis: In addition to using more than law, the first number in addition to the remainder k obtained as the first, and then use the resulting quotient divided by k, the remainder will be used as the second, and so on, until the end of business 0 .

def get_k(n, k):
    res = []
    while n > 0:
        n, m = divmod(n, k)
        s = chr(m+55) if m >= 10 else str(m)
        res.append(s)
    return ''.join(res[::-1])


print(get_k(10, 16))

 

Guess you like

Origin www.cnblogs.com/walle-zhao/p/11696179.html
Recommended