#Initialization
ni1=11010 #binary number
#Calculation
def binary_decimal(ni): # Function to convert binary to decimal
deci = 0;
i = 0;
while (ni != 0):
rem = ni-int(ni/10.)*10
ni = int(ni/10.);
deci = deci + rem*2**i;
i = i + 1;
return deci
w=binary_decimal(ni1) #calling the function
#Declaration
print'Decimal Equivalent = %f'%w
#Initialization
ni1=26 #Decimal number
#Calculation
def decimal_binary(ni): # Function to convert decimal to binary
bini = 0;
i = 1;
while (ni != 0):
rem = ni-int(ni/2)*2;
ni = int(ni/2);
bini = bini + rem*i;
i = i * 10;
return bini
w=decimal_binary(ni1) #calling the function
#Declaration
print'Binary Equivalent = %d'%w
#Initializaton
no=34.6875 #decimal number
n_int = int(no); # Extract the integral part
n_frac = no-n_int; # Extract the fractional part
#Calculation
def decimal_binary(ni): # Function to convert decimal to binary
bini = 0;
i = 1;
while (ni != 0):
rem = ni-int(ni/2)*2;
ni = int(ni/2);
bini = bini + rem*i;
i = i * 10;
return bini
def decifrac_binfrac(nf): # Function to convert binary fraction to decimal fraction
binf = 0; i = 0.1;
while (nf != 0):
nf = nf*2;
rem = int(nf);
nf = nf-rem;
binf = binf + rem*i;
i = i/10;
return binf
#Result
print "Decimal equivalent of 34.6875 = %.4f"%(decimal_binary(n_int)+decifrac_binfrac(n_frac))
#initialization
n='A013' #Hex number
#Calculation
w=int(n, 16) #Hex to Decimal Coversion
#Result
print'W = %d'%w
#Variable declaration
n=7046 #Hex number
#Calculations
h = hex(n) #decimal to hex conversion
#Result
print "The hexadecimal equivalent of 7046 is",h
#Initializaton
n='f851' #Hex Number
#Calculation
w=int(n, 16) #Hex to Decimal Coversion
def decimal_binary(ni): # Function to convert decimal to binary
bini = 0;
i = 1;
while (ni != 0):
rem = ni-int(ni/2)*2;
ni = int(ni/2);
bini = bini + rem*i;
i = i * 10;
return bini
w1=decimal_binary(w) #calling the function
#Result
print "Decimal equivalent of 34.6875 = %.d"%(w1)
#Initialiation
ni1=111011011000100 #binary number
#Calculation
def binary_decimal(ni): # Function to convert binary to decimal
deci = 0;
i = 0;
while (ni != 0):
rem = ni-int(ni/10.)*10
ni = int(ni/10.);
deci = deci + rem*2**i;
i = i + 1;
return deci
w=binary_decimal(ni1) #calling the function
h = hex(w) #decimal to hex conversion
#Result
print "The hexadecimal equivalent of 111011011000100 is",h
#initialisation
x='9450' #decimal number to be convert
#calculation
digits = [int(c) for c in x]
zero_padded_BCD_digits = [format(d, '04b') for d in digits]
#results
print "Eqivalent BCD of 72 = ",
print ''.join(zero_padded_BCD_digits)
#Initialisation
BCD="0011 1000 0111 0110" #Given BCD string
BCD_split=BCD.split(" "); #Splitting th binary string into individual BCD
d=0;
for i in range(len(BCD_split),0,-1):
d+=int(BCD_split[len(BCD_split)-i],2)*10**(i-1);
#Result
print("The equivalent decimal = %d."%d);