如何在Python 3中从base64转换为ascii

时间:2015-02-09 07:54:13

标签: python python-3.x binary ascii

我试图在python中将Base 64解码为Ascii,而不使用内置的解码功能。我能够从Ascii编码到Base 64,但是我很难回来。下面是我的编码过程。

#table to allow Base 64 Conversion
b64_table ="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"

#define variables
number = 0
numchar = 0
code = 0

#get user input
user_input = input("Input")

#loop while there is user input, ASCII -> integer
for char in user_input:
    numchar = numchar + 1
    if numchar == 1:
        number = ord(char)
    elif numchar > 1:
        number = ord(char) + (number << 8)
    #integer -> base 64
    if numchar == 3:
        i=3
        for i in (3,2,1,0):
            code = number  >> (6 * i )
            print(b64_table[int(code)]),
            #prints on one line
            number = number - (code  << (6 * i))
        numchar = 0
        #works for 3 letter multiples

0 个答案:

没有答案