Program to Convert BCD number into Decimal number | Function to convert BCD to Decimal ; Iterating through the bits backwards ; Forming the equivalent digit ( 0 to 9 ) from the group of 4. ; Reinitialize all variables and compute the number ; Update the answer ; Reverse the number formed . ; Driver Code ; Function Call
def bcdToDecimal ( s ) : NEW_LINE INDENT length = len ( s ) ; NEW_LINE check = 0 ; NEW_LINE check0 = 0 ; NEW_LINE num = 0 ; NEW_LINE sum = 0 ; NEW_LINE mul = 1 ; NEW_LINE rev = 0 ; NEW_LINE for i in range ( length - 1 , - 1 , - 1 ) : NEW_LINE INDENT sum += ( ord ( s [ i ] ) - ord ( '0' ) ) * mul ; NEW_LINE mul *= 2 ; NEW_LINE check += 1 ; NEW_LINE if ( check == 4 or i == 0 ) : NEW_LINE INDENT if ( sum == 0 and check0 == 0 ) : NEW_LINE INDENT num = 1 ; NEW_LINE check0 = 1 ; NEW_LINE DEDENT else : NEW_LINE INDENT num = num * 10 + sum ; NEW_LINE DEDENT check = 0 ; NEW_LINE sum = 0 ; NEW_LINE mul = 1 ; NEW_LINE DEDENT DEDENT while ( num > 0 ) : NEW_LINE INDENT rev = rev * 10 + ( num % 10 ) ; NEW_LINE num //= 10 ; NEW_LINE DEDENT if ( check0 == 1 ) : NEW_LINE INDENT return rev - 1 ; NEW_LINE DEDENT return rev ; NEW_LINE DEDENT if __name__ == " _ _ main _ _ " : NEW_LINE INDENT s = "100000101000" ; NEW_LINE print ( bcdToDecimal ( s ) ) ; NEW_LINE DEDENT