3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

[Joke]北斗の拳エンコード・デコード

Last updated at Posted at 2025-03-31

なんでも、ファイルを北斗の拳表現にエンコード・デコードするプログラムを書いてみました。

例えば、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
3
0
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?