forked from geekcomputers/Python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Base Converter Number system.py
84 lines (75 loc) · 2.6 KB
/
Base Converter Number system.py
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
def base_check(xnumber, xbase):
for char in xnumber[len(xnumber) - 1]:
if int(char) >= int(xbase):
return False
return True
def convert_from_10(xnumber, xbase, arr, ybase):
if int(xbase) == 2 or int(xbase) == 4 or int(xbase) == 6 or int(xbase) == 8:
if xnumber == 0:
return arr
else:
quotient = int(xnumber) // int(xbase)
remainder = int(xnumber) % int(xbase)
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, base)
elif int(xbase) == 16:
if int(xnumber) == 0:
return arr
else:
quotient = int(xnumber) // int(xbase)
remainder = int(xnumber) % int(xbase)
if remainder > 9:
if remainder == 10:
remainder = "A"
if remainder == 11:
remainder = "B"
if remainder == 12:
remainder = "C"
if remainder == 13:
remainder = "D"
if remainder == 14:
remainder = "E"
if remainder == 15:
remainder = "F"
arr.append(remainder)
dividend = quotient
convert_from_10(dividend, xbase, arr, ybase)
def convert_to_10(xnumber, xbase, arr, ybase):
if int(xbase) == 10:
for char in xnumber:
arr.append(char)
flipped = arr[::-1]
ans = 0
j = 0
for i in flipped:
ans = ans + (int(i) * (int(ybase) ** j))
j = j + 1
return ans
arrayfrom = []
arrayto = []
is_base_possible = False
number = input("Enter the number you would like to convert: ")
while not is_base_possible:
base = input("What is the base of this number? ")
is_base_possible = base_check(number, base)
if not is_base_possible:
print(f"The number {number} is not a base {base} number")
base = input
else:
break
dBase = input("What is the base you would like to convert to? ")
if int(base) == 10:
convert_from_10(number, dBase, arrayfrom, base)
answer = arrayfrom[::-1] # reverses the array
print(f"In base {dBase} this number is: ")
print(*answer, sep="")
elif int(dBase) == 10:
answer = convert_to_10(number, dBase, arrayto, base)
print(f"In base {dBase} this number is: {answer} ")
else:
number = convert_to_10(number, 10, arrayto, base)
convert_from_10(number, dBase, arrayfrom, base)
answer = arrayfrom[::-1]
print(f"In base {dBase} this number is: ")
print(*answer, sep="")