#convert decimal 12 to an octal number
def base10toN(num, base):
"""Change ``num'' to given base
Upto base 36 is supported."""
converted_string, modstring = "", ""
currentnum = num
if not 1 < base < 37:
raise ValueError("base must be between 2 and 36")
if not num:
return '0'
while currentnum:
mod = currentnum % base
currentnum = currentnum // base
converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
return converted_string
o=base10toN(12,8)
print "The procedure is as follows."
print "12 divided by 8 = quotient 1 with a remainder of 4"
print "1 divided by 8 = quotient 0 with a remainder of 1"
print "Therefore, decimal 12 = octal",o
# convert octal number to decimal.
d=int('144',8)
print "(i) octal 144 = decimal",d
d1=int("237",8)
print "(ii) octal 237 = decimal",d1
d2=int('120',8)
print "(iii) octal 120 = decimal",d2
#convert decimal to hexadecimal number
def base10toN(num, base):
"""Change ``num'' to given base
Upto base 36 is supported."""
converted_string, modstring = "", ""
currentnum = num
if not 1 < base < 37:
raise ValueError("base must be between 2 and 36")
if not num:
return '0'
while currentnum:
mod = currentnum % base
currentnum = currentnum // base
converted_string = chr(48 + mod + 7*(mod > 10)) + converted_string
return converted_string
h=base10toN(112,16)
print "The procedure is as follows,"
print "(i) 112 divided by 16 = quotient 7 with a remainder of 0"
print " 7 divided by 16 = quotient 0 with a remainder of 7"
print "decimal 112 = hex",h
print "(ii) 253 divided by 16 = quotient 7 with a remainder of 13 i.e. D"
print " 15 divided by 16 = quotient 0 with a remainder of 15 i.e. F"
h=base10toN(253,16)
print "decimal 253 = hex",h
#convert hexadecimal number to decimal
h=float.fromhex('4AB')
print "(i) hex 4AB = decimal",h
h=float.fromhex('23F')
print "(ii) hex 23F = decimal",h
# multiply binary numbers
h=int('1101',2)
o=int('1100',2)
p=h*o
z=bin(p)[2:]
print "(i) 1101 x 1100 =",z
h=int('1000',2)
o=int('101',2)
p=h*o
z=bin(p)[2:]
print "(ii) 1000 x 101 =",z
h=int('1111',2)
o=int('1001',2)
p=h*o
z=bin(p)[2:]
print "(iii) 1111 x 1001 =",z
# perform the binary divisions
x=int('110',2)
x1=int('10',2)
x2=x/x1
x3=bin(x2)[2:]
print "(i) 110 / 10"
print " = binary",x3
print " = decimal",x2
x=int('1111',2)
x1=int('110',2)
x2=x/x1
x3=bin(x2)[2:]
print "(ii) 1111 / 110"
print " = binary",x3
print " = decimal)",x2