Program to convert a Binary Number to Hexa | Function to convert BCD to hexadecimal ; Iterating through the bits backwards ; Computing the hexadecimal number formed so far and storing it in a vector . ; Reinitializing all variables for next group . ; Printing the hexadecimal number formed so far . ; Driver Code ; Function Call
def bcdToHexaDecimal ( s ) : NEW_LINE INDENT len1 = len ( s ) NEW_LINE check = 0 NEW_LINE num = 0 NEW_LINE sum = 0 NEW_LINE mul = 1 NEW_LINE ans = [ ] NEW_LINE i = len1 - 1 NEW_LINE while ( i >= 0 ) : 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 <= 9 ) : NEW_LINE INDENT ans . append ( chr ( sum + ord ( '0' ) ) ) NEW_LINE DEDENT else : NEW_LINE INDENT ans . append ( chr ( sum + 55 ) ) ; NEW_LINE DEDENT check = 0 NEW_LINE sum = 0 NEW_LINE mul = 1 NEW_LINE DEDENT i -= 1 NEW_LINE DEDENT len1 = len ( ans ) NEW_LINE i = len1 - 1 NEW_LINE while ( i >= 0 ) : NEW_LINE INDENT print ( ans [ i ] , end = " " ) NEW_LINE i -= 1 NEW_LINE DEDENT DEDENT if __name__ == ' _ _ main _ _ ' : NEW_LINE INDENT s = "100000101111" NEW_LINE bcdToHexaDecimal ( s ) NEW_LINE DEDENT