Advertisement
user_137

Baseconvert

Feb 14th, 2013
103
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.65 KB | None | 0 0
  1. def baseconvert(num, base):
  2.     '''converts an integer "num" in base 10 to given base up to 36'''
  3.     letters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
  4.     result = ''
  5.  
  6.     if num == 0:
  7.         return 0
  8.     elif num < 0:
  9.         isNeg = True
  10.     else:
  11.         isNeg = False
  12.  
  13.     while num > 0:
  14.         if num % base <= 9:
  15.             result = str(num % base) + result
  16.         else:
  17.             result = str(letters[(num % base) - 10]) + result
  18.         num = num / base
  19.     if isNeg:
  20.         result = '-' + result
  21.     return result
  22.  
  23.  
  24. number = int(raw_input('Enter a number: '))
  25. base = int(raw_input('Enter the base: '))
  26. print baseconvert(number, base)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement