-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBaseConvert.py
More file actions
33 lines (30 loc) · 932 Bytes
/
BaseConvert.py
File metadata and controls
33 lines (30 loc) · 932 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# -*- coding: utf-8 -*-
"""
Created on Mon May 20 10:30:26 2013
This program converts a sequence of decimal numbers into another base.
Author: K. Holcomb
Changelog: 2013-05-20 Initial version
"""
def convert_base(N,base):
if i==0:
conversion=str(i)
else:
digits=[]
result=i
while result>0:
digit=result%base
result=result//base
if digit<=9:
base_digit=str(digit)
else:
alpha_order=digit%10
base_digit=chr(alpha_order+65)
digits.insert(0,base_digit)
conversion="".join(digits)
return conversion
N =int(input("Please enter the maximum integer to be converted:"))
base=int(input("Please enter the target base as an integer:"))
new_base="Base "+str(base)
print(" Base 10 %s"%new_base)
for i in range(N+1):
print(" %6d %s" % (i,convert_base(i,base).rjust(6)))