Dump in a register value and get a binary representation that makes decoding registers just that bit easier.
Last active
October 4, 2023 10:08
-
-
Save calebccff/b22fdc7bfa558252a2f00d1b225daca7 to your computer and use it in GitHub Desktop.
regbits - pretty print numbers as binary
This file contains 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 | |
import argparse | |
from ansi.colour import fg | |
from ansi.colour.fx import reset | |
from math import ceil | |
def auto_int(x): | |
return int(x, 0) | |
parser = argparse.ArgumentParser(description='Pretty print') | |
parser.add_argument('-b', '--bits', type=int, default=None, help='Number of bits') | |
parser.add_argument('value', type=auto_int, help='Value to print') | |
args = parser.parse_args() | |
print("\n") | |
pretty_val = "" | |
if (args.value >> 32) > 0: | |
pretty_val += f"{fg.cyan}{args.value >> 32:#10x}" | |
pretty_val += f"{fg.green}{args.value & 0xFFFFFFFF:08x}{reset}" | |
else: | |
pretty_val += f"{fg.green}{args.value & 0xFFFFFFFF:#010x}{reset}" | |
print(f"Value: {pretty_val} // {fg.blue}{args.value}{reset}\n\n\t", end="") | |
bl = args.value.bit_length() | |
if args.bits is None or bl > args.bits: | |
args.bits = ceil(bl/8)*8 | |
cols = [fg.blue, fg.cyan, fg.green, fg.magenta, fg.yellow, fg.red] | |
coli = len(cols)-1 | |
for i in reversed(range(args.bits)): | |
if i % 8 == 7: | |
print(f"{cols[coli % len(cols)]}", end="") | |
coli -= 1 | |
if coli < 0: | |
coli = len(cols)-1 | |
print(f"{i:3d} ", end="") | |
print(f"{reset}\n", end="") | |
print("\t╭", end="") | |
print("───┬" * (args.bits - 1) , end="") | |
print("───╮\n\t│", end="") | |
for i in reversed(range(args.bits)): | |
print(" x │" if args.value & (1 << i) else " │", end="") | |
print("\n\t╰", end="") | |
print("───┴" * (args.bits - 1) , end="") | |
print("───╯\n") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment