Non-decimal radices/Convert: Difference between revisions

(added half of c)
Line 572:
<lang python>def baseN(num,b):
return ((num == 0) and "0" ) or ( baseN(num // b, b).lstrip("0") + "0123456789abcdefghijklmnopqrstuvwxyz"[num % b])
 
# alternatively:
def baseN(num,b):
if num == 0: return "0"
result = ""
while num != 0:
num, d = divmod(num, b)
result += "0123456789abcdefghijklmnopqrstuvwxyz"[d]
return result[::-1] # reverse
 
k = 26
s = baseN(k,16) # returns the string 1a
Anonymous user