-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhex_dump.py
More file actions
41 lines (34 loc) · 1.45 KB
/
hex_dump.py
File metadata and controls
41 lines (34 loc) · 1.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#Hex Dump Program
import argparse
def Main():
parser = argparse.ArgumentParser()
parser.add_argument("file", help="Specify File")
parser.add_argument("-o", "--output", help="Print output to terminal"\
, action="store_true")
args = parser.parse_args()
if args.file:
offset = 0
with open(args.file, 'rb') as infile:
with open(args.file+".dump", 'w') as outfile:
while True:
chunk = infile.read(16)
if len(chunk) == 0:
break
text = str(chunk)
text = ''.join([i if ord(i) < 128 and ord(i) > 32 else '.' for i in text])
output = "{:#08x}".format(offset) + ": "
output += " ".join("{:02X}".format(ord(c)) for c in chunk[:8])
output += " | "
output += " ".join("{:02X}".format(ord(c)) for c in chunk[8:])
if len(chunk) % 16 != 0:
output += " "*(16 - len(chunk)) + text
else:
output += " " + text
if args.output:
print (output)
outfile.write(output + '\n')
offset += 16
else:
print (parser.usage)
if __name__ == '__main__':
Main()