Skip to content

Instantly share code, notes, and snippets.

@juliangroen
Created February 1, 2023 07:24
Show Gist options
  • Save juliangroen/fe22acc56aec56af505746db1788b8bb to your computer and use it in GitHub Desktop.
Save juliangroen/fe22acc56aec56af505746db1788b8bb to your computer and use it in GitHub Desktop.
chatGPT created python script to convert binary file to hex output similar to xxd
import sys
def read_binary_file_to_hex(filename):
with open(filename, 'rb') as binary_file:
data = binary_file.read()
hex_data = data.hex()
block_size = 32
offset = 0
for i in range(0, len(hex_data), block_size):
block = hex_data[i:i + block_size]
formatted_block = ' '.join(block[j:j + 4].upper() for j in range(0, len(block), 4))
ascii_block = "".join(
[
chr(int(block[j:j + 2], 16))
if 32 <= int(block[j:j + 2], 16) <= 126
else "."
for j in range(0, len(block), 2)
]
)
print(f'{offset:08x}: {formatted_block} {ascii_block}')
offset += 0x10
if __name__ == '__main__':
if len(sys.argv) != 2:
print('Usage: python script.py <binary_file>')
sys.exit(1)
filename = sys.argv[1]
read_binary_file_to_hex(filename)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment