Last active
January 19, 2023 13:11
-
-
Save wakusei-meron-/d292193e35fb844c19c9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{"110000": "w", "110001": "x", "110101": "1", "110100": "0", "010100": "U", "010101": "V", "001100": "M", "001101": "N", "011110": "e", "011111": "f", "001001": "J", "001000": "I", "011011": "b", "011010": "a", "000110": "G", "000111": "H", "000011": "D", "000010": "C", "100100": "k", "100101": "l", "111100": "8", "111101": "9", "100010": "i", "100011": "j", "101110": "u", "101111": "v", "111001": "5", "111000": "4", "101011": "r", "101010": "q", "110011": "z", "110010": "y", "010010": "S", "010011": "T", "010111": "X", "010110": "W", "110110": "2", "110111": "3", "011000": "Y", "011001": "Z", "001111": "P", "001110": "O", "011101": "d", "011100": "c", "001010": "K", "001011": "L", "101101": "t", "000000": "A", "000001": "B", "100111": "n", "100110": "m", "000101": "F", "000100": "E", "111111": "/", "111110": "+", "100001": "h", "100000": "g", "010001": "R", "010000": "Q", "101100": "s", "111010": "6", "111011": "7", "101000": "o", "101001": "p"} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import io | |
import json | |
def main(): | |
dict = {} | |
n = 0 | |
# A~Z | |
for i in range(ord('A'), ord('Z') + 1): | |
char = unichr(i) | |
bin = format(n, 'b').zfill(6) | |
dict[str(bin)] = char | |
print bin, char | |
n += 1 | |
# a~z | |
for i in range(ord('a'), ord('z') + 1): | |
char = unichr(i) | |
bin = format(n, 'b').zfill(6) | |
dict[str(bin)] = char | |
print bin, char | |
n += 1 | |
# 0~9 | |
for i in range(0, 10): | |
bin = format(n, 'b').zfill(6) | |
dict[str(bin)] = str(i) | |
print bin, i | |
n+=1 | |
# + | |
bin = format(n, 'b').zfill(6) | |
dict[str(bin)] = '+' | |
n+=1 | |
bin = format(n, 'b').zfill(6) | |
dict[str(bin)] = '/' | |
f = open('base64_table.json', "w") | |
f.write(json.dumps(dict)) | |
f.close() | |
if __name__ == '__main__': | |
main() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import sys | |
import json | |
# 1文字が8バイトで表される | |
BYTE_SIZE = 8 | |
# 文字列textをバイナリの文字列に変換する | |
def str2bin(s): | |
binStr = "" | |
#ord関数で文字をASCIIコードへ変換し、 | |
#シフト演算で各桁のビットを取り出す | |
for c in s: | |
for i in range(BYTE_SIZE): | |
binStr += str((ord(c) >> (BYTE_SIZE - (i + 1))) & 1) | |
return binStr | |
# 文字列sを文字数nで分割したリストを返す | |
# (例) | |
# split("abcdef", 2) => [["ab"], ["cd"], ["ef"]] | |
def split(s, n): | |
return [s[i:i+n] for i in range(0, len(s), n)] | |
# 文字列sを文字数nで分割した時に足りない部分を文字cで埋める | |
# (例) | |
# fillInBlank("abcd", 6, "==") => "abcd==" | |
def fillInBlank(s, n, c): | |
mod = len(s) % n | |
# 割り切れた場合は何も処理をしない | |
if mod == 0: return s | |
# 割り切れなかった場合、残りの部分を埋める | |
margin = n - mod | |
return s + c * margin | |
# main関数 | |
def main(): | |
# コマンドの引数を受け取る | |
argvs = sys.argv | |
argc = len(argvs) | |
# 引数が1つじゃなかったら無かったら、処理をせず終了 | |
if argc != 2: | |
print "Usage:\n$ python %s CONVERT_STRING" % argvs[0] | |
quit() | |
# 1. 文字列をバイナリ文字列に変換 | |
binStr = str2bin(argvs[1]) | |
# 2. バイナリを6ビットづつに分割 | |
splitCount =6 | |
s = split(binStr, splitCount) | |
# 最後の2ビットが余るので,6ビットになるように0を追加する | |
s[-1] = fillInBlank(s[-1], 6, "0") | |
# 変換表の辞書を読み込み | |
tableFile = open("base64_table.json", "r") | |
base64Dict = json.loads(tableFile.read()) | |
result = "" | |
for i in s: | |
result += base64Dict[i] | |
print result | |
print fillInBlank(result, 4, "=") | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment