-
Notifications
You must be signed in to change notification settings - Fork 1
/
conver.py
executable file
·47 lines (44 loc) · 1.4 KB
/
conver.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
#-------------------------------------------------------------------------------
# Name: convert
# Purpose:
#
# Author: mejonzhan
#
# Created: 01/01/2015
# Copyright: (c) Administrator 2015
# Licence: <your licence>
# decimal convert any base like binary, oct, hex, etc.
#-------------------------------------------------------------------------------
import sys
def main():
decimal = eval(input("please input the decimal for converting binary: "))
base = eval(input("please input base: "))
decimalToNBaseByNormal(decimal, base)
decToNBaseByRecursion(decimal, base)
def decimalToNBaseByNormal(decimalVar, base):
tempList = []
temp = decimalVar
i = 0
while (temp > 0):
ord = temp % base
if (ord > 9):
ord = chr(65 + (ord - 10))
tempList.append(ord)
temp = int(temp / base)
i = i + 1
tempList.reverse();
#print(tempList)
binary = ""
for j in range(len(tempList)):
binary = binary + str(tempList[j]);
print("the decimal is: %d and after convering by %d base is %s"%(decimalVar, base, binary))
def decToNBaseByRecursion(dec, base):
if (dec == 0):
return
decToNBaseByRecursion(int(dec/base), base)
ord = dec % base
if (ord > 9):
ord = chr(65 + (ord - 10))
sys.stdout.write(str(ord))
if __name__ == '__main__':
main()