#!/usr/bin/env python3

import sys

def hex_digit(y):
    '''0-9 unchanged, 10-15 -> abcdef'''
    # note that this would work also for bases
    #  11, 12, 13, ..., 16, 17, 18, ..., 36
    if y < 10:
        return str(y)
    else:
        return chr(ord('a') + (y-10))

#print(hex_digit(5))
#print(hex_digit(15))
#sys.exit()
        
def convert(x, b):
    place_value = 1
    value_string = ''
    while x > 0:
        digit = hex_digit(x % b)
        value_string = str(digit) + value_string
        #print(f'{place_value}s digit: {digit}')
        x = x // b # chops off the last digit
        place_value *= b
    return value_string

#print(convert(80, 5))
#sys.exit()

def convert10(x):
    '''show how to get each digit out of x'''
    # x = 456
    # 1s digit: 6
    # 10s digit: 5
    # 100s digit: 4

    place_value = 1
    value_string = ''
    while x > 0:
        digit = x % 10
        value_string = str(digit) + ' ' + value_string
        print(f'{place_value}s digit: {digit}')
        x = x // 10 # chops off the last digit
        place_value *= 10
    print(value_string)
    # was 456, now x = 456 // 10 # 45

if __name__ == '__main__':
    # if running the program like
    #     python3 bases.py
    # or
    #     ./bases.py
    # not like >>> import bases
    #convert10(456)
    #convert(94, 16)
    #sys.exit()

    
    if len(sys.argv) < 3:
        print('Usage: ./bases.py x b')
        print('')
        print('  prints the decimal number x in base b')
        sys.exit()

    print(convert(int(sys.argv[1]), int(sys.argv[2])))
