diff --git a/over/misc.py b/over/misc.py index 4167191..fd9f13c 100644 --- a/over/misc.py +++ b/over/misc.py @@ -191,3 +191,31 @@ def hexdump(data, indent=0, offset=16, show_header=True, show_offsets=True, show if not output: output_io.seek(0) return output_io.read() + +# -------------------------------------------------- + +def raw_to_hex(raw, spaces=True): + """ + Converts a bytearray (or bytes) into its textual hexadecimal representation. + """ + + output = [] + + for o in raw: + output.append(hex(o)[2:].zfill(2)) + + return (" " if spaces else "").join(output) + +def hex_to_raw(text): + """ + Converts a hexadecimal representation of a byte array into a bytearray. + """ + + output = [] + + text = text.replace(" ", "") + + for i in range(len(text)//2): + output.append(int(text[2*i:2*i+2], 16)) + + return bytearray(output)