Make the code slightly more readable

This commit is contained in:
Nick Chambers 2024-05-03 13:13:13 -05:00
parent 5c413c1947
commit 5bf1c56f54
1 changed files with 7 additions and 4 deletions

View File

@ -1,5 +1,8 @@
import sys
def padlen(number):
return ((16 - (number % 16)) * 3) + 1 + int(number % 16 < 8)
def hexdump(data):
printed = []
@ -8,7 +11,7 @@ def hexdump(data):
for idx in range(len(data)):
print(f"{data[idx]:02x}", end=" ")
if data[idx] < 129 and chr(data[idx]).isprintable():
if data[idx] < 128 and chr(data[idx]).isprintable():
printed.append(chr(data[idx]))
else:
printed.append(".")
@ -17,12 +20,12 @@ def hexdump(data):
print(end=" ")
if idx == len(data) - 1:
fmt = ""
padding = ""
if (idx + 1) % 16 != 0:
fmt = " " * (((16 - ((idx + 1) % 16)) * 3) + 1 + int((idx + 1) % 16 < 8))
padding = " " * padlen(idx + 1)
print(f"{fmt}|{''.join(printed)}|")
print(f"{padding}|{''.join(printed)}|")
print(f"{idx + 1:08x}")
elif (idx + 1) % 16 == 0:
print(f"|{''.join(printed)}|")