python convert hex (2-62 decimal)

python convert hex (2-62 arbitrary binary conversion)

First talk about 62 hexadecimal expressions:
0-9 0-9 still continues decimal expression and lowercase letters az, then capital letters AZ
i.e. 35 [10 Hex] = z [62 Hex] ; 61 [10 hex] = Z [62 hex];
as with letters, thus defining a string input

#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的进制,需要转换的进制)

Results are as follows:
zF4mOFpN7A
zF [62 is][ 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 ]
[ 62 ] 6
7 [ 10 ][ 62 ] 7
8 [ 10 ]
[ 62 ] 8
9 [ 10 ][ 62 ] 9
10 [ 10 ]
[ 62 ] a
11 [ 10 ][ 62 ] b
12 [ 10 ]
[ 62 ] c
13 [ 10 ][ 62 ] d
14 [ 10 ]
[ 62 ] e
15 [ 10 ][ 62 ] f
16 [ 10 ]
[ 62 ] g
17 [ 10 ][ 62 ] h
18 [ 10 ]
[ 62 ] i
19 [ 10 ][ 62 ] j
20 [ 10 ]
[ 62 ] k
21 [ 10 ][ 62 ] l
22 [ 10 ]
[ 62 ] m
23 [ 10 ][ 62 ] n
24 [ 10 ]
[62] the
25 [10][ 62 ] p
26 [ 10 ]
[ 62 ] q
27 [ 10 ][ 62 ] r
28 [ 10 ]
[ 62 ] s
29 [ 10 ][ 62 ] t
30 [ 10 ]
[62] to
31 [10][62] v
32 [10]
[ 62 ] w
33 [ 10 ][ 62 ] x
34 [ 10 ]
[62] and
35 [10][62] of
36 [10]
[ 62 ] A
37 [ 10 ][ 62 ] B
38 [ 10 ]
[ 62 ] C
39 [ 10 ][ 62 ] D
40 [ 10 ]
[ 62 ] E
41 [ 10 ][ 62 ] F
42 [ 10 ]
[ 62 ] G
43 [ 10 ][ 62 ] H
44 [ 10 ]
[ 62 ] I
45 [ 10 ][ 62 ] J
46 [ 10 ]
[ 62 ] K
47 [ 10 ][ 62 ] L
48 [ 10 ]
[ 62 ] M
49 [ 10 ][ 62 ] N
50 [ 10 ]
[62] The
51 [10][ 62 ] P
52 [ 10 ]
[ 62 ] Q
53 [ 10 ][ 62 ] R
54 [ 10 ]
[ 62 ] S
55 [ 10 ][ 62 ] T
56 [ 10 ]
[62] At
57 [10][62] V
58 [10]
[ 62 ] W
59 [ 10 ][ 62 ] X
60 [ 10 ]
[62] Y
61 [10] == [62] From

Used as follows: f (str, str radix needs to be converted to hexadecimal), as f ( 'zf', 62,16) that is represented by 62 hexadecimal ZF, into a number expressed in hexadecimal

Extensions:
as long as the list of extended characters to a [] in, may be used, such as kanji 'One', 'B, CD', 'Zichou Yinmao' can.

Released seven original articles · won praise 0 · Views 184

Guess you like

Origin blog.csdn.net/weixin_45903952/article/details/104073949