練習問題/解答例/Base64/Python
をテンプレートにして作成
[
トップ
] [
新規
|
一覧
|
検索
|
最終更新
|
ヘルプ
|
ログイン
]
開始行:
[[練習問題]]
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from string import ascii_lowercase, ascii_uppercase
table = ascii_uppercase + ascii_lowercase + ''.join(map(...
def binstr(n, maxbits=None):
absn = abs(n)
s = [ chr(((n>>b)&1)+48) for b in range(maxbits or l...
return ''.join(reversed(s))
def encode(filename):
with open(filename, 'rb') as F:
bit = ''.join(binstr(ord(x), 8) for x in F.read())
sz = len(bit)
fill = 6 - sz % 6
bit += '0' * fill
sz += fill
res = ''.join(table[int(bit[i:i+6], 2)] for i in ran...
return res + '=' * (4 - len(res) % 4)
def decode(filename):
with open(filename, 'rb') as F:
bit = ''.join(format(table.find(x), 'b').zfill(6...
sz = len(bit)
extra = 0
while (sz-extra) % 8 > 0 and bit[-extra-1] == '0':
extra += 1
bit = bit[:-extra-1]
sz -= extra
res = ''.join(chr(int(bit[i:i+8], 2)) for i in range...
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...
print(res)
elif option == '-d':
res = decode(sys.argv[2])
sys.stdout.write(res)
else:
sys.stderr.write('Huh?\n')
終了行:
[[練習問題]]
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
from string import ascii_lowercase, ascii_uppercase
table = ascii_uppercase + ascii_lowercase + ''.join(map(...
def binstr(n, maxbits=None):
absn = abs(n)
s = [ chr(((n>>b)&1)+48) for b in range(maxbits or l...
return ''.join(reversed(s))
def encode(filename):
with open(filename, 'rb') as F:
bit = ''.join(binstr(ord(x), 8) for x in F.read())
sz = len(bit)
fill = 6 - sz % 6
bit += '0' * fill
sz += fill
res = ''.join(table[int(bit[i:i+6], 2)] for i in ran...
return res + '=' * (4 - len(res) % 4)
def decode(filename):
with open(filename, 'rb') as F:
bit = ''.join(format(table.find(x), 'b').zfill(6...
sz = len(bit)
extra = 0
while (sz-extra) % 8 > 0 and bit[-extra-1] == '0':
extra += 1
bit = bit[:-extra-1]
sz -= extra
res = ''.join(chr(int(bit[i:i+8], 2)) for i in range...
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...
print(res)
elif option == '-d':
res = decode(sys.argv[2])
sys.stdout.write(res)
else:
sys.stderr.write('Huh?\n')
ページ名: