python 转换进制(2-128进制)

∥☆過路亽.° 提交于 2020-01-23 10:38:43

python 转换进制(2-128进制任意转换)

因为先做的62进制程序,所以先说62进制的方法,扩展到128进制的见后面的部分,基本一样
首先说说62进制的表达方式:
0-9依然延续10进制的0-9表达,然后是小写的字母a-z,再大写的字母A-Z
即35[10进制]=z[62进制];61[10进制]=Z[62进制];
因为带字母,因此定义以字符串形式输入
转换方法,先切片字符为list,然后转换为10进制的数字,再转换为要转换进制的表达式。
其中reverse() 函数用于反向列表中元素,由个十百转为百十个

#coding=gbk
def f(nx,x1,x):
    #n为待转换的十进制数,x为机制,取值为2-62
    a=['0','1','2','3','4','5','6','7','8','9',
    'a','b','c','d','e','f','g','h','i','j','k',
    'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K',
    'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z']
    nx=str(nx)
    b1=list(nx)
    print (nx,"[",x1,"]==[",x,"] ",end='') 
    b2=[]
    for i in b1:
        for i1 in range(0,62):
            if a[i1]==i:
                b2=b2+[i1]
                if i1>x1:
                    print (i,"错误定义")
    b2.reverse()
    #print(b2)
    n1=0
    n2=1
    for i in b2:
        n1=n1+int(i)*(pow(x1,n2-1))  #pow(x, n),即计算 x 的 n 次幂函数
        n2=n2+1
        #print (n1,n2)
    n=n1
    #print(n)
    b=[]
    while True:
        s=n//x#商
        y=n%x#余数
        b=b+[y]
        if s==0:
            break
        n=s
    b.reverse()  #reverse() 函数用于反向列表中元素,由个,十百转为百十个
    bd=""
    for i in b:
        #print(a[i],end='')
        bd=bd+a[i]
    print (bd)
    return bd

chun='zF4mOFpN7A' 
print (chun)
print (f(chun[0:2],62,10)+f(chun[2:6],62,10)+f(chun[6:11],62,10))
for i in range(0,62):
	f(str(i),10,62)
#F(str,str的进制,需要转换的进制)

运行结果如下:
zF4mOFpN7A
zF [ 62 ][ 10 ] 2211
4mOF [ 62 ]
[ 10 ] 1041021
pN7A [ 62 ][ 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 ][ 62 ] T
56 [ 10 ]
[ 62 ] U
57 [ 10 ][ 62 ] V
58 [ 10 ]
[ 62 ] W
59 [ 10 ][ 62 ] X
60 [ 10 ]
[ 62 ] Y
61 [ 10 ]==[ 62 ] Z

用法如下:f(str,str的进制,需要转换的进制),如f(‘zf’,62,16)即表示62进制的zf,转化为16进制的表达数

程序的扩展:
只要把a[]中的list扩展字符即可,如可使用的汉字’壹’,‘甲乙丙丁’,'子丑寅卯’都可以。

索性来个128位进制的,加入小写大写罗马,汉字数字,天干地支,东西南北,(再加就得中发白,梅兰竹菊了)

#coding=gbk
def f(nx,x1,x):
    #nx为待转换的x1进制数,x1为转换前进制,x为转换完进制,取值为2-128位
    a=[
    '0','1','2','3','4','5','6','7','8','9',
    'a','b','c','d','e','f','g','h','i','j','k',
    'l','m','n','o','p','q','r','s','t','u','v','w','x','y','z',
    'A','B','C','D','E','F','G','H','I','J','K',
    'L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z',
    'ⅰ','ⅱ','ⅲ','ⅳ','ⅴ','ⅵ','ⅶ','ⅷ','ⅸ','ⅹ',
    'Ⅰ','Ⅱ','Ⅲ','Ⅳ','Ⅴ','Ⅵ','Ⅶ','Ⅷ','Ⅸ','Ⅹ',
    '○','一','二','三','四','五','六','七','八','九',
    '零','壹','贰','叁','肆','伍','陆','柒','捌','玖',
    '甲','乙','丙','丁','戊','己','庚','辛','壬','癸',
    '子','丑','寅','卯','辰','巳','午','未','申','酉','戌','亥',
    '东','南','西','北'
    ]
    #可再扩展字符,如汉字'壹','甲乙丙丁','子丑寅卯'
    nx=str(nx)
    b1=list(nx)
    print (nx,"[",x1,"]==[",x,"] ",end='') 
    b2=[]
    for i in b1:
        for i1 in range(0,len(a)):
            if a[i1]==i:
                b2=b2+[i1]
                if i1>x1:
                    print (i,"错误定义")
    b2.reverse()
    #print(b2)
    n1=0
    n2=1
    for i in b2:
        n1=n1+int(i)*(pow(x1,n2-1))  #pow(x, n),即计算 x 的 n 次幂函数
        n2=n2+1
        #print (n1,n2)
    n=n1
    #print(n)
    b=[]
    while True:
        s=n//x#商
        y=n%x#余数
        b=b+[y]
        if s==0:
            break
        n=s
    b.reverse()  #reverse() 函数用于反向列表中元素,由个,十百转为百十个
    bd=""
    for i in b:
        #print(a[i],end='')
        bd=bd+a[i]
    print (bd)
    return bd

chun='zF4mOFpN7A' 
print (chun)
print (f(chun[0:2],62,10)+f(chun[-8:-4],62,10)+f(chun[-4:],62,10))
#for i in range(0,128):
#	f(str(i),10,128)
f('12壹Ⅰⅲ东甲32',128,121)
#F(str,str的进制,需要转换的进制)

运行
f(‘12壹Ⅰⅲ东甲32’,128,121)
12壹Ⅰⅲ东甲32 [ 128 ]==[ 121 ] 1Ⅰ肆Ⅴ午C贰Ⅵⅰ

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!