Skip to content

Instantly share code, notes, and snippets.

@iwanders
Last active August 17, 2025 20:52
Show Gist options
  • Select an option

  • Save iwanders/c9320aa48666d2a20776b49fe973005f to your computer and use it in GitHub Desktop.

Select an option

Save iwanders/c9320aa48666d2a20776b49fe973005f to your computer and use it in GitHub Desktop.
Hextool: commandline utility to convert hex, binary and decimal.
#!/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]
input_hexdump = "fd83ea"
# 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 and vice versa
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), ""), "")
# Space hex to ascii joined.
def H2AJ(a):
return j(d2a(h2d(s(a))), "")
example_print("H2AJ", H2AJ, j(d2h(input_text)), "")
# decimal to unicode
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")
# Unicode to decimal
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), "")
# Space hext to unicode joined
def H2UJ(a):
return j(d2u(h2d(s(a))), "")
example_print("H2UJ", H2UJ, j(d2h(input_text[0:3] + input_unicode)), "")
# Split in pairs, for concatenated hex strings.
def s2(s):
s = s.replace(" ", "").replace("\n", "").replace("\r", "")
if len(s) % 2 != 0:
print("length is not correct, cowardly bailing")
return None
values = []
for i in range(int(len(s)/2)):
entry = s[i*2:(i+1)*2]
# values.append(int(entry, 16))
values.append(entry)
return values
example_print("s2", s2, input_hexdump, "")
# s2 hex split to decimal.
def s2hd(s):
return h2d(s2(s))
example_print("s2hd", s2hd, input_hexdump, "")
# Decimal to c array 0xaa
def d2c(d):
if len(d) == 0:
return ""
return ", ".join(f"0x{v:0>2x}" for v in d)
example_print("d2c", d2c, s2hd(input_hexdump), "")
# Decimal to C array 0xAA
def d2C(d):
if len(d) == 0:
return ""
return ", ".join(f"0x{v:0>2X}" for v in d)
example_print("d2C", d2C, s2hd(input_hexdump), "")
print("hexdoc() reprints this.")
def hexdoc():
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)), "")
example_print("s2", s2, input_hexdump, "")
example_print("s2hd", s2hd, input_hexdump, "")
example_print("d2c", d2c, s2hd(input_hexdump), "")
example_print("d2C", d2C, s2hd(input_hexdump), "")
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