Conversion of numbers- Python codes
Description:
The following Python codes are for the conversion of numbers from one base to another. Base can be any number between 2 (binary) and 16 (hexadecimal). The main code is “nsysconv.py”, which uses functions defined in the other codes (also supplied). These codes were written based on the descriptions in Number Systems and Their Decimal Equivalents, and Decimal Number Conversion to Other Number Systems. They are provided “as is”. Modification may be required in order to achieve users’ other purposes (such as expanding the codes for bases greater than 16). For the main code to work properly, the class and function files must be named correctly, i.e., “calc.py” and “funs.py”, respectively, and placed in the same directory (folder) as the main code.
Python codes
# main - file name: nsysconv.py
from calc import *
num = input("Enter the number to be converted: ")
b1 = int(input("Enter the number's base: "))
b2 = int(input("Enter the new base (from 2 to 16): "))
# Check if the new base if different from the entered number's base
if b2 == b1:
print("\nError: the two number systems have the same base")
exit()
# Check whether the entered number is consistent with its base
for d in num:
if d != '.':
if (myInt(d)>=b1) or (myInt(d)<0):
print("\nError, the entered number is outside of the base's range")
exit()
# Start conversion
x = Calc(b1,b2,num)
if b2 == 10:
new_n = x.b2_10()
print("The equivalent decimal number is:",end=" ")
print(new_n)
elif b1==10:
new_n = x.b1_10()
print("Equivalent value in the new number system is:",end=" ")
print(new_n)
else:
n = x.b2_10()
new_n = Calc(b1,b2,str(n)).b1_10()
print("Equivalent value in the new number system is:",end=" ")
print(new_n)
# file name: calc.py
from funs import *
class Calc:
def __init__(self,b1,b2,num):
self.b1 = b1
self.b2 = b2
self.num = num
self.prd = False
self.num1 = []
self.num2 = []
def checkn(self):
# Check if there is a fractional part in the number
cnt = 0
n = self.num
n1 = self.num1
n2 = self.num2
pd = self.prd
for d in n:
if d != ".":
cnt += 1
elif d==".":
pd = True
break
if pd == True:
n1 = n[0:cnt]
n2 = n[cnt+1:]
return pd,n1,n2
def b2_10(self):
pd, n1, n2 = Calc.checkn(self)
n = self.num
b1 = self.b1
b2 = self.b2
if pd == True:
l = len(n1)
x = 0.0
for d in n1:
l -= 1
x += myInt(d)*b1**(l)
for d in n2:
l -= 1
x += myInt(d)*b1**(l)
else :
l = len(n)
x = 0
for d in n:
l -= 1
x += myInt(d)*b1**(l)
return x
def b1_10(self):
pd, n1, n2 = Calc.checkn(self)
n = self.num
b1 = self.b1
b2 = self.b2
if pd == True:
tol = 1e-6
l2 = len(n2)
k = l2 # num of digits after the whole number
nn1 = int(n1)
fn = float(n)
nn2 = fn - nn1
# whole number
q,r = divmod(nn1,b2)
x = mySym(r)
while (q>0):
nn = q
q, r =divmod(nn,b2)
x = mySym(r) + x
# Fractional part
m = 1/b2
q,r =divmod(nn2,m)
x = x + "." + mySym(int(q))
i=1
while (r>tol) and (i<k):
nn = r
m *= (1/b2)
q, r =divmod(nn,m)
x += mySym(int(q))
i += 1
# print(x)
else :
nn = int(n)
# whole number
q,r = divmod(nn,b2)
x = mySym(r)
while (q>0):
nn = q
q, r =divmod(nn,b2)
x = mySym(r) + x
# print(x)
return x
# file name: funs.py
def myInt(x):
switcher = {
'0': 0,
'1': 1,
'2': 2,
'3': 3,
'4': 4,
'5': 5,
'6': 6,
'7': 7,
'8': 8,
'9': 9,
'A': 10,
'a': 10,
'B': 11,
'b': 11,
'C': 12,
'c': 12,
'D': 13,
'd': 13,
'E': 14,
'e': 14,
'F': 15,
'f': 15,
}
return switcher.get(x, "out of range")
def mySym(x):
switcher = {
0: '0',
1: '1',
2: '2',
3: '3',
4: '4',
5: '5',
6: '6',
7: '7',
8: '8',
9: '9',
10: 'A',
11: 'B',
12: 'C',
13: 'D',
14: 'E',
15: 'F',
}
return switcher.get(x, "out of range")
