Skip to content

Instantly share code, notes, and snippets.

@dmazin
Created July 19, 2023 13:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dmazin/7348f61312c647e79e75eb6e8521ef68 to your computer and use it in GitHub Desktop.
Save dmazin/7348f61312c647e79e75eb6e8521ef68 to your computer and use it in GitHub Desktop.
def format_hex_dump(dump_str):
# Split string into lines
lines = dump_str.split('\n')
# Create a list to hold all bytes
all_bytes = []
# Process each line
for line in lines:
# Split the line into address and bytes
address, bytes_str = line.split(':')
# Split the bytes string into individual bytes
bytes_list = bytes_str.split()
# Strip leading "0x" from each byte and add it to the list
all_bytes.extend([byte[2:] for byte in bytes_list])
# Group all bytes by 16
grouped_bytes = [all_bytes[i:i+16] for i in range(0, len(all_bytes), 16)]
# Join bytes in each group with spaces and groups with line breaks
formatted_dump = '\n'.join([' '.join(group) for group in grouped_bytes])
return formatted_dump
format_hex_dump(dump_str.strip()) # strip leading/trailing whitespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment