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

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

首先说说62进制的表达方式:
0-9依然延续10进制的0-9表达,然后是小写的字母a-z,再大写的字母A-Z
即35[10进制]=z[62进制];61[10进制]=Z[62进制];
因为带字母,因此定义以字符串形式输入

#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 ][ 10 ] 6147026
221110410216147026
0 [ 10 ]
[ 62 ] 0
1 [ 10 ][ 62 ] 1
2 [ 10 ]
[ 62 ] 2
3 [ 10 ][ 62 ] 3
4 [ 10 ]
[ 62 ] 4
5 [ 10 ][ 62 ] 5
6 [ 10 ]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转换的进制),如f(‘zf’,62,16)即表示62进制的zf,转化为16进制的表达数

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

python 转换进制(2-62进制)python 转换进制(2-62进制) weixin_45903952 发布了7 篇原创文章 · 获赞 0 · 访问量 191 私信 关注
上一篇:64匹马,8个赛道,找出前4名最少比赛多少场?——最快10次,最慢11次;


下一篇:算法初学(第四天)4-1