Navigation

Iterative Conversions Recursive Conversions

Iterative Number Base Conversions

1. Decimal to Binary

def decimal_to_binary(dec_num): bin_num = '' while dec_num > 0: remainder = dec_num % 2 bin_num = str(remainder) + bin_num dec_num = dec_num // 2 return bin_num # main print(decimal_to_binary(1114)) # Output: 10001011010 --------------------------------------- 10001011010

2. Decimal to Hexadecimal

def decimal_to_hexadecimal(dec_num): dec_hex_map = {1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', \ 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'} hex_num = '' while dec_num > 0: remainder = dec_num % 16 hex_num = str(dec_hex_map[remainder]) + hex_num dec_num = dec_num // 16 return hex_num # main print(decimal_to_hexadecimal(1114)) # Output: 45A --------------------------------------- 45A

3. Decimal to Base n, where n is an integer

def decimal_to_base(dec_num, n): num = '' while dec_num > 0: remainder = dec_num % n num = str(remainder) + num dec_num = dec_num // n return num # main print(decimal_to_base(1114, 8)) # Output: 2132 --------------------------------------- 2132

4. Binary to Decimal

def binary_to_decimal(bin_num): dec_num = 0 for i in range(len(bin_num)): digit = bin_num[len(bin_num) - 1 - i] dec_num += int(digit) * (2 ** i) return dec_num # main print(binary_to_decimal('10001011010')) # Output: 1114 --------------------------------------- 1114

5. Hexadecimal to Decimal

def hexadecimal_to_decimal(hex_num): hex_dec_map = {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \ 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15} dec_num = 0 for i in range(len(hex_num)): digit = hex_num[len(hex_num) - 1 - i] dec_num += int(hex_dec_map[digit]) * (16 ** i) return dec_num # main print(hexadecimal_to_decimal('45A')) # Output: 1114 --------------------------------------- 1114

You only need these 5 as you can use it to convert from one base to denary to another base. For example: binary to decimal to hexadecimal

6. Binary to Hexadecimal (OPTIONAL)

def binary_to_hexadecimal(bin_num): bin_hex_map = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', \ '0100': '4', '0101': '5', '0110': '6', '0111': '7', \ '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', \ '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'} bin_num = bin_num.zfill((len(bin_num) + 3) // 4 * 4) hex_num = '' for i in range(0, len(bin_num), 4): hex_num += bin_hex_map[bin_num[i:i+4]] return hex_num # main print(binary_to_hexadecimal('10001011010')) # Output: 45A --------------------------------------- 45A

7. Hexadecimal to Binary (OPTIONAL)

def hexadecimal_to_binary(hex_num): hex_bin_map = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', \ '4': '0100', '5': '0101', '6': '0110', '7': '0111', \ '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', \ 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'} bin_num = ''.join(hex_bin_map[digit.upper()] for digit in hex_num) return bin_num # main print(hexadecimal_to_binary('45A')) # Output: 10001011010 --------------------------------------- 10001011010

Recursive Number Base Conversions

1. Decimal to Binary (Recursive)

def decimal_to_binary_recursive(dec_num): if dec_num == 0: return '' else: return decimal_to_binary_recursive(dec_num // 2) + str(dec_num % 2) # main print(decimal_to_binary_recursive(1114)) # Output: 10001011010 --------------------------------------- 10001011010

2. Decimal to Hexadecimal (Recursive)

dec_hex_map = {1:'1', 2:'2', 3:'3', 4:'4', 5:'5', 6:'6', 7:'7', 8:'8', 9:'9', \ 10:'A', 11:'B', 12:'C', 13:'D', 14:'E', 15:'F'} def decimal_to_hexadecimal_recursive(dec_num): if dec_num == 0: return '' else: remainder = dec_num % 16 return decimal_to_hexadecimal_recursive(dec_num // 16) + str(dec_hex_map[remainder]) # main print(decimal_to_hexadecimal_recursive(1114)) # Output: 45A --------------------------------------- 45A

3. Binary to Decimal (Recursive)

def binary_to_decimal_recursive(bin_num): if len(bin_num) == 0: return 0 else: digit = bin_num[0] return int(digit) * (2 ** (len(bin_num) - 1)) + binary_to_decimal_recursive(bin_num[1:]) # main print(binary_to_decimal_recursive('10001011010')) # Output: 1114 --------------------------------------- 1114

4. Hexadecimal to Decimal (Recursive)

hex_dec_map = {'1':1, '2':2, '3':3, '4':4, '5':5, '6':6, '7':7, '8':8, '9':9, \ 'A':10, 'B':11, 'C':12, 'D':13, 'E':14, 'F':15} def hexadecimal_to_decimal_recursive(hex_num): if len(hex_num) == 0: return 0 else: digit = hex_num[0] return int(hex_dec_map[digit]) * (16 ** (len(hex_num) - 1)) + hexadecimal_to_decimal_recursive(hex_num[1:]) # main print(hexadecimal_to_decimal_recursive('45A')) # Output: 1114 --------------------------------------- 1114

5. Binary to Hexadecimal (Recursive)

bin_hex_map = {'0000': '0', '0001': '1', '0010': '2', '0011': '3', \ '0100': '4', '0101': '5', '0110': '6', '0111': '7', \ '1000': '8', '1001': '9', '1010': 'A', '1011': 'B', \ '1100': 'C', '1101': 'D', '1110': 'E', '1111': 'F'} def binary_to_hexadecimal_recursive(bin_num): padding = len(bin_num) % 4 if padding != 0: bin_num = '0' * (4 - padding) + bin_num if len(bin_num) == 0: return '' else: group = bin_num[-4:] return binary_to_hexadecimal_recursive(bin_num[:-4]) + bin_hex_map[group] # main print(binary_to_hexadecimal_recursive('10001011010')) # Output: 45A --------------------------------------- 45A

6. Hexadecimal to Binary (Recursive)

hex_to_bin_map = {'0': '0000', '1': '0001', '2': '0010', '3': '0011', \ '4': '0100', '5': '0101', '6': '0110', '7': '0111', \ '8': '1000', '9': '1001', 'A': '1010', 'B': '1011', \ 'C': '1100', 'D': '1101', 'E': '1110', 'F': '1111'} def hexadecimal_to_binary_recursive(hex_num): if len(hex_num) == 0: return '' else: return hex_to_bin_map[hex_num[0].upper()] + hexadecimal_to_binary_recursive(hex_num[1:]) # main print(hexadecimal_to_binary_recursive('45A')) # Output: 10001011010 --------------------------------------- 10001011010