def c32encode(input_hex, min_length=None):
    """
    >>> c32encode('a46ff88886c2ef9762d970b4d2c63678835bd39d')
    'MHQZH246RBQSERPSE2TD5HHPF21NQMWX'
    >>> c32encode('')
    ''
    >>> c32encode('0000000000000000000000000000000000000000', 20)
    '00000000000000000000'
    >>> c32encode('0000000000000000000000000000000000000001', 20)
    '00000000000000000001'
    >>> c32encode('1000000000000000000000000000000000000001', 32)
    '20000000000000000000000000000001'
    >>> c32encode('1000000000000000000000000000000000000000', 32)
    '20000000000000000000000000000000'
    >>> c32encode('1')
    '1'
    >>> c32encode('22')
    '12'
    >>> c32encode('001')
    '01'
    >>> c32encode('0001')
    '01'
    >>> c32encode('00001')
    '001'
    >>> c32encode('000001')
    '001'
    >>> c32encode('10')
    'G'
    >>> c32encode('100')
    '80'
    >>> c32encode('1000')
    '400'
    >>> c32encode('10000')
    '2000'
    >>> c32encode('100000')
    '10000'
    >>> c32encode('1000000')
    'G0000'
    """
    if len(input_hex) == 0:
        return ''

    if not re.match(r'^[0-9a-fA-F]+$', input_hex):
        raise ValueError('Requires a hex string')

    if len(input_hex) % 2 != 0:
        input_hex = '0{}'.format(input_hex)

    input_hex = input_hex.lower()
    
    res = []
    carry = 0
    for i in range(len(input_hex) - 1, -1, -1):
        if (carry < 4):
            current_code = HEX.index(input_hex[i]) >> carry
            next_code = 0
            if i != 0:
                next_code = HEX.index(input_hex[i-1])

            # carry = 0, next_bits is 1, carry = 1, next_bits = 2
            next_bits = 1 + carry
            next_low_bits = (next_code % (1 << next_bits)) << (5 - next_bits)
            cur_c32_digit = C32[current_code + next_low_bits]
            carry = next_bits
            res = [cur_c32_digit] + res
        else:
            carry = 0

    # fix padding
    # -- strip leading c32 zeros
    # -- add leading hex zeros
    c32_leading_zeros = 0
    for i in range(0, len(res)):
        if res[i] != '0':
            break

        c32_leading_zeros += 1

    res = res[c32_leading_zeros:]

    num_leading_hex_zeros = 0
    num_leading_byte_zeros = 0
    for i in range(0, len(input_hex)):
        if input_hex[i] != '0':
            break

        num_leading_hex_zeros += 1

    num_leading_byte_zeros = num_leading_hex_zeros / 2
    res = ['0'] * num_leading_byte_zeros + res

    if min_length > 0:
        count = min_length - len(res)
        if count > 0:
            res = ['0'] * count + res

    return ''.join(res)
>>> c32encode('a46ff88886c2ef9762d970b4d2c63678835bd39d')
    'MHQZH246RBQSERPSE2TD5HHPF21NQMWX'
    >>> c32encode('')
    ''
    >>> c32encode('0000000000000000000000000000000000000000', 20)
    '00000000000000000000'
    >>> c32encode('0000000000000000000000000000000000000001', 20)
    '00000000000000000001'
    >>> c32encode('1000000000000000000000000000000000000001', 32)
    '20000000000000000000000000000001'
    >>> c32encode('1000000000000000000000000000000000000000', 32)
    '20000000000000000000000000000000'
    >>> c32encode('1')
    '1'
    >>> c32encode('22')
    '12'
    >>> c32encode('001')
    '01'
    >>> c32encode('0001')
    '01'
    >>> c32encode('00001')
    '001'
    >>> c32encode('000001')
    '001'
    >>> c32encode('10')
    'G'
    >>> c32encode('100')
    '80'
    >>> c32encode('1000')
    '400'
    >>> c32encode('10000')
    '2000'
    >>> c32encode('100000')
    '10000'
    >>> c32encode('1000000')
    'G0000'