python进制转换

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
# @Time       : 2020/12/12 20:58
# @Author     : ENDZHT
# @ProjectName: pythonExercise
# @FileName   : intBaseConversion.py
# @Software   : PyCharm

"""
    实现任意进制转换
"""


def base_convert(num, start_base=10, end_base=10):
    """
    数字进制任意转换
    :param num:
    :param start_base: 原进制,默认为10
    :param end_base: 目标进制,默认为10
    :return: 转换后的进制数
    """
    # 进制对应符号表
    base_map1 = {0: '0', 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', 16: 'G', 17: 'H', 18: 'I', 19: 'J', 20: 'K', 21: 'L', 22: 'M',
                 23: 'N', 24: 'O', 25: 'P', 26: 'Q', 27: 'R', 28: 'S', 29: 'T', 30: 'U', 31: 'V', 32: 'W', 33: 'X',
                 34: 'Y', 35: 'Z', 36: 'a', 37: 'b', 38: 'c', 39: 'd', 40: 'e', 41: 'f', 42: 'g', 43: 'h', 44: 'i',
                 45: 'j', 46: 'k', 47: 'l', 48: 'm', 49: 'n', 50: 'o', 51: 'p', 52: 'q', 53: 'r', 54: 's', 55: 't',
                 56: 'u', 57: 'v', 58: 'w', 59: 'x', 60: 'y', 61: 'z', 62: '@', 63: '#'}

    base_map2 = {'0': 0, '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, 'G': 16, 'H': 17, 'I': 18, 'J': 19, 'K': 20, 'L': 21, 'M': 22,
                 'N': 23, 'O': 24, 'P': 25, 'Q': 26, 'R': 27, 'S': 28, 'T': 29, 'U': 30, 'V': 31, 'W': 32, 'X': 33,
                 'Y': 34, 'Z': 35, 'a': 36, 'b': 37, 'c': 38, 'd': 39, 'e': 40, 'f': 41, 'g': 42, 'h': 43, 'i': 44,
                 'j': 45, 'k': 46, 'l': 47, 'm': 48, 'n': 49, 'o': 50, 'p': 51, 'q': 52, 'r': 53, 's': 54, 't': 55,
                 'u': 56, 'v': 57, 'w': 58, 'x': 59, 'y': 60, 'z': 61, '@': 62, '#': 63}

    # 判断进制格式,支持2-64进制转换
    if not ((2 <= start_base <= 64) and (2 <= end_base <= 64)):
        print("支持2-64进制转换")
        exit(0)

    # 判断输入数字的进制
    if num[:2].lower() == '0b':
        start_base = 2
    elif num[:2].lower() == '0o':
        start_base = 8
    elif num[:2].lower() == '0x':
        start_base = 16
    else:
        start_base = start_base

    # 将输入数字转换为10进制
    def to_10_base(num, start_base):
        num = num[::-1]
        result = 0
        i = 0
        for x in num:
            result += base_map2[x] * (start_base ** (i))
            i += 1
        return result

    num = to_10_base(num, start_base)
    # if 2 <= start_base <= 36:
    #     num = int(num, base=start_base)
    # else:
    #     num = to_10_base(num)

    print("10十进制:%d" % num)

    # num转换为end_base进制
    def to_end_base(num, end_base):
        stack = []
        while num != 0:
            stack.append(base_map1[num % end_base])
            num //= end_base
        result = ''
        print(stack)
        while stack:
            print(stack)
            result += stack.pop()
        return result

    return to_end_base(num, end_base)

    # python内置函数demo
    if start_base == 10:
        if end_base == 2:
            return bin(num)
        elif end_base == 8:
            return oct(num)
        elif end_base == 10:
            return num

    elif end_base == 10:
        return int(num, base=start_base)


def demo_bin(num):
    """
    将num转换为2进制
    :param num: [2进制|8进制|10进制|16进制]
    :return: num的2进制数字
    """
    return bin(num)


def demo_oct(num):
    """
    将num转换为8进制
    :param num: [2进制|8进制|10进制|16进制]
    :return: num的8进制数字
    """
    return oct(num)


def demo_hex(num):
    """
    将num转换为16进制
    :param num: [2进制|8进制|10进制|16进制]
    :return: num的16进制数字
    """
    return hex(num)


def demo_int(num, base=10):
    """
    int()如果使用base参数时,num不能输入数字类型
    :param num:
    :param base:
    :return:
    """
    return int(num, base=base)


def get_base_map():
    """
    生成basemap,进制与对应的符号
    :return:
    """
    str_base = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz@#'
    basemap1 = dict()
    basemap2 = dict()
    for i in range(64):
        basemap1[i] = str_base[i]
        basemap2[str_base[i]] = i

    print(basemap1)
    print(basemap2)


if __name__ == '__main__':
    print(base_convert('1000000', 10, 63))
    print(base_convert("3@y1", 63, 10))
    # print(type(demo_bin(0b10)))
    # print(demo_int("11", 36))
    # get_base_map()
    pass