-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbin2hex.py
49 lines (35 loc) · 935 Bytes
/
bin2hex.py
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
42
43
44
45
46
47
48
49
# small python script to dump a binary file to an array of hex bytes
# corelanc0d3r - www.corelan-consulting.com
import sys, textwrap
if len(sys.argv) != 2:
print "usage: bin2hex.py <file>"
sys.exit()
print "Reading %s" % sys.argv[1]
infile = open(sys.argv[1],'rb')
content = infile.read()
infile.close()
nullsCount = 0
out = ""
totalbytes = 0
for c in content:
byte = ord(c)
out += "\\x%02x" % byte
totalbytes += 1
if byte == 0:
nullsCount += 1
print "Read %d bytes" % totalbytes
print """
--------------------------------------------
Displaying bytes as hex :
--------------------------------------------
"""
for line in textwrap.wrap(out, 32):
print "\"%s\"" % line
print "\nNumber of nulls : %d" % nullsCount
print """\n
--------------------------------------------
Displaying bytes as ascii :
--------------------------------------------
"""
for line in textwrap.wrap(content,50):
print line