본문 바로가기
problem solving

1928. Base64 Decoder

by ormgm 2019. 9. 14.

t = int(input())
Base64 = ['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','0','1','2','3','4','5','6','7','8','9','+','/']
for p in range(t):
    strr = input() # 문자열 입력
    ans = ''
    for q in range(0,len(strr),+4):#인코딩 문자 4개씩 집어넣기
        four = strr[q]+strr[q+1]+strr[q+2]+strr[q+3] #인코딩 문자 4개
        num = [0]*4 # 문자와 대조되는 값
        binn = [[0 for i in range(6)]for j in range(4)]  #binn[4][6]
        binn_8 = [['' for i in range(8)]for j in range(3)] #2진수 6비트를 8비트로 묶기위한 배열
        dec = [0]*3 #10진수 저장 배열
        dex = [128,64,32,16,8,4,2,1]#2진수를 10진수로 바꾸기위한배열
        for i in range(0,4,+1): # 2진수로 바꾸는 과정
            num[i] = Base64.index(four[i])
            while num[i] != 0:
                if num[i] - 32 >= 0:
                    num[i] = num[i]-32
                    binn[i][0] = binn[i][0] + 1
                elif num[i] - 16 >= 0:
                    num[i] = num[i]-16
                    binn[i][1] = binn[i][1] + 1
                elif num[i] - 8 >= 0:
                    num[i] = num[i]-8
                    binn[i][2] = binn[i][2] + 1
                elif num[i] - 4 >= 0:
                    num[i] = num[i]-4
                    binn[i][3] = binn[i][3] + 1
                elif num[i] - 2 >= 0:
                    num[i] = num[i]-2
                    binn[i][4] = binn[i][4] + 1
                elif num[i] - 1 >= 0:
                    num[i] = num[i]-1
                    binn[i][5] = binn[i][5] + 1
        binn_8[0] = (binn[0]) + (binn[1][:2])
        binn_8[1] = (binn[1][2:]) + (binn[2][:4])
        binn_8[2] = (binn[2][4:]) + (binn[3])
        for i in range(3): # 10진수(아스키코드)로 바꾸기
            for j in range(8):
                dec[i] = dec[i] + (binn_8[i][j]*dex[j])
        for i in range(3):# 아스키 코드를 
            char = chr(dec[i])#char에 아스키코드를 문자로 변환한 값저장
            ans = ans + char

댓글