Skip to content

Instantly share code, notes, and snippets.

@peta909
Created February 15, 2019 08:36
Show Gist options
  • Save peta909/648e8f401ec57c3299e4650fe8a1ee60 to your computer and use it in GitHub Desktop.
Save peta909/648e8f401ec57c3299e4650fe8a1ee60 to your computer and use it in GitHub Desktop.
Simple hex dump using python. using binascii and struct modules
#try to write a simple hex dump
import binascii,struct
fd = open("abcd.exe", "r")
fd_contents_str = fd.read()
fd_contents_hex = (binascii.b2a_hex(fd_contents_str)).upper()
Hex_dump = []
Byte_str = ""
for i, Half_byte in enumerate(fd_contents_hex):
Byte_str = Byte_str + Half_byte
if i % 2:
Byte_hex = struct.unpack('2b', Byte_str)#"4D" converted into (52,68)
Hex_dump.append(Byte_hex)
Byte_str = ""
print Hex_dump
fd.close()
@peta909
Copy link
Author

peta909 commented Feb 15, 2019

simple hex dumper using python
demo use of modules in binascii and struct for manipulating hex values and bytes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment