Problem Description
对于任意二进制文件(如图像、声音、视频、程序等),都可以用base64编码。
base64编码方法:先把二进制代码划分为一系列24位长的单元,然后把每一个24位单元划分为4个6位的组。每一个6位组按下列方法转换为ASCII码。6位二进制有64个不同的值,0-63。用A表示0,B表示1,等。26个大写字母排列完毕后,再用26个小写字母,然后再用10个数字,最后用+表示62,用/表示63。当要编码的个数不是3的倍数时,等号符“=”被用作填充符。
例如:
24位二进制代码:01001001 00110001 01111001
划分为4个6位组:010010 010011 000101 111001
对应的base64编码: S T F 5
对应的base64编码的二进制代码:01010011 01010100 01000110 00110101
Input Description
输入经过base64编码的文本,不超过10000个字符。
Output Description
输出经过base64解码后的文本。
Sample Input
I2luY2x1ZGUgPHN0ZGlvLmg+CmludCBtYWluKCkKewogICAgaW50IGEsYjsKICAgIHNjYW5mKCIlZCAlZCIsJmEsICZiKTsKICAgIHByaW50ZigiJWRcbiIsYStiKTsKICAgIHJldHVybiAwOwp9Cj09
Sample Output
#include <stdio.h>
int main()
{
int a,b;
scanf("%d %d",&a, &b);
printf("%d\n",a+b);
return 0;
}
Hint
我的想法:
我的代码:
# -*- coding=utf-8 -*-
# @Time: 2023/3/11 10:26
# @Author: Tan Mingjia
# @File: base64decode.py
# @Software: PyCharm
def decode64(src):
keystr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
base = {ch:keystr.find(ch) for ch in keystr}
length = len(src)
if length % 4 != 0:
print("error!\n")
return
i = 0
dst = []
while i < length:
ench1 = base[src[i]]
i += 1
ench2 = base[src[i]]
i += 1
ench3 = base[src[i]]
i += 1
ench4 = base[src[i]]
i += 1
ch1 = (ench1 << 2) | (ench2 >> 4)
ch2 = ((ench2 & 15) << 4) | (ench3 >> 2)
ch3 = ((ench3 & 3) << 6) | ench4
dst.append(ch1)
dst.append(ch2)
dst.append(ch3)
dst = list(map(chr, dst))
if dst[-1] == '=':
dst.pop()
if dst[-1] == '=':
dst.pop()
dst = "".join(dst)
return dst
if __name__ == "__main__":
src = input()
dst = decode64(src)
print(dst)