なんでも、ファイルを北斗の拳表現にエンコード・デコードするプログラムを書いてみました。
例えば、0x01というコードがあるとすると、それは、0b00000001なので、1=あ、0=たに対応させて、「あたたたたたたた」となります。下位ビットから見るので。
"あ"を"0"、"た"を"1"に置き換えると、下位ビットが先頭に来る一つの2進数を表すファイルを扱うようになります。
このプログラムは、ファイル名にenhokuto
という文字列が含まれていたら、エンコード、含まれていなかったら、デコードをします。そのため、ln -s enhokuto.py dehokuto.py
として、シンボリックリンクをしてお使いください。
enhokuto.pyは、file.hokutoという北斗の拳ファイルを出力します。
dehokuto.pyは、file.hokuto.resultという、北斗の拳ファイルをデコードした元ファイルのコピーを出力します。
enhokuto.py
#!/usr/bin/env python3
import sys
def enhokuto(mem):
s=""
for i in mem:
for k in range(8):
s+="あ" if not i&(1<<k) else "た"
return s
def dehokuto(s):
d=[]
b=0
for k in range(len(s)):
if s[k]=="た":
b=b|(1<<(k%8))
if k%8==7:
d+=[b]
b=0
return d
if __name__=='__main__':
if len(sys.argv)<2:
exit(-1)
if "enhokuto" in sys.argv[0]:
f=open(sys.argv[1],"rb")
src=enhokuto(list(f.read()))
f.close()
f=open(sys.argv[1]+".hokuto","w")
f.write(src)
f.close()
else:
f=open(sys.argv[1],"r")
mem=dehokuto(f.read())
f.close()
f=open(sys.argv[1]+".result","wb")
f.write(bytes(mem))
f.close()
f.close()
実行例
$ echo "test">test
$ cat test
test
$ ls
dehokuto.py@ enhokuto.py* test
$ ./enhokuto.py test
$ ls
dehokuto.py@ enhokuto.py* test test.hokuto
$ cat test.hokuto
ああたあたたたあたあたああたたあたたああたたたあああたあたたたああたあたああああ
$ ./dehokuto.py test.hokuto
$ ls
dehokuto.py@ enhokuto.py* test test.hokuto test.hokuto.result
$ cat test.hokuto.result
test