Advertisement
imk0tter

Imk0tter

Nov 19th, 2010
164
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.52 KB | None | 0 0
  1. def base(number,baseFrom,baseTo):
  2.     if baseFrom == 10: return __base(long(number),baseTo)
  3.     return __base(_base(str(number).lower(),baseFrom,len(str(number))),baseTo)
  4.    
  5. def _base(num,b1,len,mod=0):
  6.     charset = string.digits + string.ascii_lowercase
  7.     try:
  8.         out = charset.index(num[len-1])*b1**mod
  9.     except:
  10.         out = 0
  11.     return out if len == 1 else out + _base(num,b1,len-1,mod+1)
  12.    
  13. def __base(num,b2):
  14.     charset = string.digits + string.ascii_uppercase
  15.     y = num%b2
  16.     out = charset[y]
  17.     return out if num == y else __base((num-y)/b2,b2) + out
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement