Last active
August 28, 2023 21:43
-
-
Save iwanders/c9320aa48666d2a20776b49fe973005f to your computer and use it in GitHub Desktop.
Hextool: commandline utility to convert hex, binary and decimal.
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/env python3 | |
def example_print(call, fun, input, desc): | |
inputs = "{}({:})".format(call, repr(input)) | |
outputs = "{:>8s}".format(repr(fun(input))) | |
print("{: <30s} -> {: >30s} {:s}".format(inputs, outputs, desc)) | |
input_byte = 0x3D | |
input_list = [0x38, input_byte] | |
input_text = [0x68, 0x65, 0x6c, 0x6c, 0x6f] | |
input_unicode = [0xe2, 0x80, 0x99] | |
# decimal to binary | |
def d2b(a): | |
if (type(a) == int): | |
return "{:0>8b}".format(a) | |
else: | |
return [d2b(z) for z in a] | |
example_print("d2b", d2b, input_byte, "") | |
example_print("d2b", d2b, input_list, "") | |
# binary to decimal | |
def b2d(a): | |
if (type(a) == str): | |
return int(a, 2) | |
if (type(a) == int): | |
return a | |
else: | |
return [b2d(z) for z in a] | |
example_print("b2d", b2d, d2b(input_byte), "") | |
example_print("b2d", b2d, d2b(input_list), "") | |
# decimal to hex | |
def d2h(a): | |
if (type(a) == int): | |
return "{:0>2X}".format(a) | |
if type(a) == str: | |
return d2h(int(a)) | |
return [d2h(z) for z in a] | |
example_print("d2h", d2h, input_byte, "") | |
example_print("d2h", d2h, input_list, "") | |
# hex to decimal | |
def h2d(a): | |
if (type(a) == str): | |
return int(a, 16) | |
if (type(a) == int): | |
return a | |
return [h2d(z) for z in a] | |
example_print("h2d", h2d, d2h(input_byte), "") | |
example_print("h2d", h2d, d2h(input_list), "") | |
def h2b(a): | |
return d2b(h2d(a)) | |
example_print("h2b", h2b, d2h(input_byte), "") | |
example_print("h2b", h2b, d2h(input_list), "") | |
def b2h(a): | |
return d2h(b2d(a)) | |
example_print("b2h", b2h, d2b(input_byte), "") | |
example_print("b2h", b2h, d2b(input_list), "") | |
# reverse string | |
def rev(a): | |
if (type(a) == str): | |
return a[::-1] | |
else: | |
return [rev(z) for z in a] | |
example_print("rev", rev, d2b(input_byte), "") | |
example_print("rev", rev, d2h(input_list), "") | |
example_print("rev", rev, d2b(input_list), "") | |
def j(a, sep=" "): | |
return sep.join(a) | |
example_print("j", j, d2b(input_list), "") | |
def s(a, sep=" "): | |
return a.split(sep) | |
example_print("s", s, j(d2b(input_list)), "") | |
example_print("s", s, j(d2h(input_list)), "") | |
# Decimal to ascii | |
def d2a(a): | |
if type(a) == str: | |
return d2a(int(a)) | |
if type(a) == int: | |
return chr(a) | |
else: | |
return [d2a(z) for z in a] | |
def a2d(a): | |
if type(a) == list: | |
return [a2d(z) for z in a] | |
if type(a) == str and len(a) != 1: | |
return a2d([c for c in a]) | |
else: | |
return ord(a) | |
example_print("d2a", d2a, input_text, "") | |
example_print("a2d", a2d, d2a(input_text), "") | |
example_print("a2d", a2d, j(d2a(input_text), ""), "") | |
def H2AJ(a): | |
return j(d2a(h2d(s(a))), "") | |
example_print("H2AJ", H2AJ, j(d2h(input_text)), "") | |
def d2u(a): | |
if type(a) == str: | |
return d2u(int(a)) | |
if type(a) == int: | |
return chr(a) | |
else: | |
return bytes([z for z in a]).decode("utf-8") | |
def u2d(a): | |
if type(a) == list: | |
return bytes([u2d(z) for z in a]) | |
else: | |
return [x for x in a.encode('utf-8')] | |
example_print("d2u", d2u, input_unicode, "") | |
example_print("u2d", u2d, d2u(input_unicode), "") | |
def H2UJ(a): | |
return j(d2u(h2d(s(a))), "") | |
example_print("H2UJ", H2UJ, j(d2h(input_text[0:3] + input_unicode)), "") | |
print("doc() reprints this.") | |
def doc(): | |
example_print("d2b", d2b, input_byte, "") | |
example_print("d2b", d2b, input_list, "") | |
example_print("b2d", b2d, d2b(input_byte), "") | |
example_print("b2d", b2d, d2b(input_list), "") | |
example_print("d2h", d2h, input_byte, "") | |
example_print("d2h", d2h, input_list, "") | |
example_print("h2d", h2d, d2h(input_byte), "") | |
example_print("h2d", h2d, d2h(input_list), "") | |
example_print("h2b", h2b, d2h(input_byte), "") | |
example_print("h2b", h2b, d2h(input_list), "") | |
example_print("b2h", b2h, d2b(input_byte), "") | |
example_print("b2h", b2h, d2b(input_list), "") | |
example_print("rev", rev, d2b(input_byte), "") | |
example_print("rev", rev, d2h(input_list), "") | |
example_print("rev", rev, d2b(input_list), "") | |
example_print("j", j, d2b(input_list), "") | |
example_print("s", s, j(d2b(input_list)), "") | |
example_print("s", s, j(d2h(input_list)), "") | |
example_print("d2a", d2a, input_text, "") | |
example_print("a2d", a2d, d2a(input_text), "") | |
example_print("a2d", a2d, j(d2a(input_text), ""), "") | |
example_print("H2AJ", H2AJ, j(d2h(input_text)), "") | |
import rlcompleter | |
import readline | |
readline.parse_and_bind("tab: complete") | |
import code | |
code.interact(local=locals()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment