-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathconvertNumber.py
104 lines (88 loc) · 3.19 KB
/
convertNumber.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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python
""" Arguments
sys.argv[0] = filename
sys.argv[1] = number
sys.argv[2] = source
sys.argv[3] = destination
"""
import sys
import alp
import string
ALPHA = string.digits + string.uppercase + string.lowercase + '+' + '/'
def base64_encode(num, base, alphabet=ALPHA):
"""Encode a number in Base X
`num`: The number to encode
`base`: Base of number
`alphabet`: The alphabet to use for encoding
"""
if (num == 0):
return alphabet[0]
arr = []
while num:
rem = num % base
num = num // base
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def base64_decode(string, base, alphabet=ALPHA):
"""Decode a Base X encoded string into the number
Arguments:
- `string`: The encoded string
- `base`: base of number
- `alphabet`: The alphabet to use for encoding
"""
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base ** power)
idx += 1
return num
if (len(sys.argv) == 4 and sys.argv[3] != "1"):
# calculate integer first
if (int(sys.argv[2]) <= 36):
# use built in python conversion if possible
decimal = int(sys.argv[1], int(sys.argv[2]))
elif (int(sys.argv[2]) > 36 and int(sys.argv[2]) <= 64):
# otherwise, use base64_decode
decimal = base64_decode(sys.argv[1], int(sys.argv[2]))
else:
# create dictionary to create xml from it
errorDic = dict(title="Ohoh, your number couldn't be converted", subtitle="make sure your base is between 2 and 64", uid="error", valid=False)
e = alp.Item(**errorDic)
alp.feedback(e)
sys.exit()
# create dictionary to create xml from it
decimalDic = dict(title=str(decimal), subtitle="Decimal", uid="decimal", valid=True, arg=str(decimal), icon="icons/decimal.png")
d = alp.Item(**decimalDic)
# calculate new number
if (int(sys.argv[3]) >= 2 and int(sys.argv[3]) <= 64):
conv = base64_encode(decimal, int(sys.argv[3]))
else:
# create dictionary to create xml from it
errorDic = dict(title="Ohoh, your number couldn't be converted", subtitle="make sure your base is between 2 and 64", uid="error", valid=False)
e = alp.Item(**errorDic)
itemsList = [d, e]
alp.feedback(itemsList)
sys.exit()
# create dictionary to create xml from it
convertDic = dict(title=conv, subtitle="Number to base " + str(sys.argv[3]), uid="conv", valid=True, arg=conv)
c = alp.Item(**convertDic)
if (int(sys.argv[2]) >= 36 or int(sys.argv[3]) >= 36):
# create dictionary to create xml from it
infoDic = dict(title="Case-Sensitive", subtitle="Be aware, if base is >= 36 letters are case-sensitive", uid="conv", valid=True, arg=conv)
i = alp.Item(**infoDic)
itemsList = [d, c, i]
else:
itemsList = [d, c]
alp.feedback(itemsList)
else:
if (len(sys.argv) != 4):
errorDic = dict(title="Make sure to pass 3 numbers", subtitle="convert `number` `base of original` `base to convert to`", uid="error", valid=False, arg="error")
error = alp.Item(**errorDic)
alp.feedback(error)
elif (int(sys.argv[3]) == 1):
errorDic = dict(title="Base 1 makes no sense", subtitle="", uid="error", valid=False, arg="error")
error = alp.Item(**errorDic)
alp.feedback(error)