1
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?

キーコードを表示したり、式の値を16進、8進、2進、10進で表示したり、ASCII-CODE表を表示したりするターミナルアシスタント

Last updated at Posted at 2025-04-24

キーコードを表示したり、式の値を16進、8進、2進、10進で表示したり、ASCII-CODE表を表示したりするターミナルアシスタントです。
^Aでアスキーコード表示、:で式の評価をして各進数で表示、キーのタイプでキーコードを表示します。
^Cでquitです。

assist.py
#!/usr/bin/python3
import sys,tty
import termios

def getch():
    fd = sys.stdin.fileno()
    old_settings = termios.tcgetattr(fd)
    tty.setraw(sys.stdin.fileno())
    ch = sys.stdin.read(1)
    termios.tcsetattr(fd, termios.TCSADRAIN, old_settings)
    return ch

while True:
    print("-"*70)
    key=getch()
    if key==chr(3):
        break
    k=ord(key)
    print(f"{key} : 0x{k:02X} 0o{k:03o} 0b{k:08b} {k}" ,flush=True)
    if key==chr(1):
        for i in range(256):
            print(f"{'\''+chr(i)+'\'' if 0x20<=i<=0x7e else ('^'+chr(0x40+i)+" " if i<=0x1f else ' . ')} : 0x{i:02X} 0o{i:03o} 0b{i:08b} {i:>3d}  ",end='')
            if i%2==1:
                print("")
        print("")
    if key==':':
        print("expression: prefix 0x - hexadecimal 0o - octal 0b - binary none - decimal")
        a=input(":")
        c=eval(a)
        print(f"{c} : 0x{c:016X} 0o{c:024o}")
        print(f"0b{c:064b}")

exit(0)

chmod +x assist.pyで実行権限を付け、./assist.pyで実行して下さい。

この際作っておこう。eval()は便利ですなあ。

1
0
0

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
1
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?