-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder.py
67 lines (59 loc) · 1.48 KB
/
encoder.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
#DEBAYAN MAJUMDER 2020
#Version 3.1
#Bug Fixes: Binary conversion was not appropriate.
#THIS PYTHON SCRIPT HAS THREE FUNCTIONS WHICH HELPS TO RETURN THE EQUIVALENT
#OF THE NUMBER PASSED.
#THE SCRIPT CONVERTS DECIMAL TO THREE DIFFERENT BASES
#METHOD FOR CONVERTING DECIMAL TO BINARY
def toBinary(n):
values = [64, 32, 16, 8, 4, 2, 1]
output = []
sum = 0
i = 0
c = 0
while c != len(values):
temp = sum + values[i]
if(temp <= n):
sum = sum + values[i]
output.append("1")
i = i + 1
else:
output.append("0")
i = i + 1
c = c + 1
return "".join(output)
#METHOD FOR CONVERTING DECIMAL TO HEXADECIMAL
def toHexadecimal(n):
output = []
i = 0
while(n != 0):
temp = 0
temp = n % 16
if(temp < 10):
output.append(chr(temp + 48))
i = i + 1
else:
output.append(chr(temp + 55))
i = i + 1
n = int(n / 16)
output.reverse()
return "".join(output)
#METHOD FOR CONVERTING DECIMAL TO OCTAL
def toOctal(n):
output = []
while n!=0:
output.append(str(n % 8))
n = int(n/8)
output.reverse()
return "".join(output)
#METHOD TO EXTRACT FILE NAME FROM A PATH PASSED
def extractFileName(path):
i = 0
j = 0
for this in path:
if this == "/":
j = i
elif this == ".":
break
i = i + 1
return path[j+1:i]