Decimal to binary conversion
# Algorithm: In addition to the reverse modulo 2; integer divided by 2, you get a quotient and a remainder, continue with business divisible by 2, until the quotient is 0; # and then the remainder taken out from the bottom up, that is, the converted binary results DEF devTobin (NUM): result = [] the while . 1 : result.append (STR (NUM % 2 )) NUM = NUM // 2 IF NUM == 0: BREAK return '' .join (result [:: - . 1 ]) Print (devTobin (789))
Binary Coded Decimal
# 110 = 0 + 0 * 1 * 2 ^ 1 ^ 2 ^ 2 + 2 * 1 = 6 # fetch backwards from DEF binTodev (binnum): Result = 0 num_length = len (STR (binnum)) for I in Range (num_length): Result + = int (str (binnum) [- (. 1 + I)]) * POW (2 , I) # Note conversion between str and int return Result Print (binTodev (110))