練習問題
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from string import ascii_lowercase, ascii_uppercase
table = ascii_uppercase + ascii_lowercase + ''.join(map(str, range(10))) + '+/'
def binstr(n, maxbits=None):
absn = abs(n)
s = [chr(((n>>b)&1)+48) for b in range(maxbits or len(hex(n))*4) if maxbits or absn>>b or not b or (n<0 and absn>>(b-1)) ]
return ''.join(reversed(s))
def encode(filename):
bit = ''
with open(filename, 'rb') as F:
R = F.read()
bit += ''.join(binstr(ord(x), 8) for x in R)
sz = len(bit)
fill = 6 - sz%6
bit += '0' * fill
sz += fill
res = ''.join(table[int(bit[i:i+6], 2)] for i in range(0, sz, 6))
return res + '=' * (4 - len(res)%4)
def decode(filename):
bit = ''
with open(filename, 'rb') as F:
R = F.read()
bit += ''.join(format(table.find(x), 'b').zfill(6) for x in R if x in table)
while len(bit) % 8 > 0 and bit[-1] == '0':
bit = bit[:-1]
sz = len(bit)
res = ''.join(chr(int(bit[i:i+8], 2)) for i in range(0, sz, 8))
return res
N = 76
option = sys.argv[1]
res = ''
if option == '-e':
encoded = encode(sys.argv[2])
res = '\n'.join(encoded[i:i+N] for i in range(0, len(encoded), N))
print(res)
elif option == '-d':
res = decode(sys.argv[2])
sys.stdout.write(res)
else:
sys.stderr.write('Huh?\n')