-
-
Save rfrancoise/54d4f581c44eb9511011396893bdb852 to your computer and use it in GitHub Desktop.
Python reimplementation of iprint
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/python3 | |
# Reimplementation of i.c (https://packages.debian.org/iprint) | |
import curses.ascii | |
import sys | |
escapes = ['0', 0, 0, 0, 0, 0, 0, 'a', 'b', 't', 'n', 'v', 'f', 'r'] | |
def iprint(number): | |
sys.stdout.write("{} 0x{:X} 0{:o} 0b{:b}".format(*[number] * 4)) | |
if curses.ascii.isascii(number): | |
if number < len(escapes) and escapes[number]: | |
sys.stdout.write(" '\{}'".format(escapes[number])) | |
elif curses.ascii.isprint(number): | |
sys.stdout.write(" '{:c}'".format(number)) | |
sys.stdout.write('\n') | |
for arg in sys.argv[1:]: | |
if arg.startswith("0x"): | |
iprint(int(arg[2:], 16)) | |
elif arg.startswith("0b"): | |
iprint(int(arg[2:], 2)) | |
elif arg.startswith("0"): | |
iprint(int(arg[1:], 8)) | |
elif arg.isalpha(): | |
for letter in arg: | |
iprint(int(ord(letter))) | |
else: | |
iprint(int(arg)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment