-
Notifications
You must be signed in to change notification settings - Fork 2
/
hexdump.py
executable file
·127 lines (103 loc) · 2.49 KB
/
hexdump.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
#!/usr/bin/env python
#
# $Id$
#
# implemented a couple of useful functions for displaying binary data.
#
def tobin(i):
"""Convert an int to a binary
This function converts an int into a string representing the int
in binary.
e.g. tobin(170) would produce 10101010
the string is left padded with 0's to the nearest 32, 16, or 8 bit boundry.
e.g.:
170 10101010
342 0000000101010110
65537 00000000000000010000000000000001
"""
s = ""
max = 8
if i > 255:
max = 16
if i > 65535:
max = 32
if i > 2**32:
max = 64
bins = range(max)
bins = map(lambda a: 2 ** a, bins)
bins.reverse()
for b in bins:
if b & i == b:
s+= "1"
else:
s+= "0"
return s
def hexdump(str, prepend = '', start = 0):
"""Hexdump's things
The prepend arg is prepended to every string thats printed out,
useful if you want to indent stuff, it defaults to ''.
"""
x = y = count = 0
indent = 16
out = ''
print "%s" % (' ' * (len(prepend) + 6)),
for i in range(start % indent, indent):
# for i in range(0, indent):
print "%02x" % (i),
if start != 0:
for i in range(0, start % indent):
print "%02x" % (i),
print
out = ''
while (count < len(str)):
out = "%s%04x : " % (prepend, start + count)
x = 0
while x < indent:
if (x + count + 1 ) > len(str):
break
out = out + "%02x " % (ord(str[x + count]))
if (x + count + 1) >= len(str):
x += 1
y = 0
for y in range(indent - x):
out = out + ' '
break
x += 1
out = out + ": "
x = 0
while x < indent:
if (x + count + 1) > len(str):
break
if ord(str[x + count]) >= 32 and ord(str[x + count]) <= 126:
out = out + str[x + count]
elif ord(str[x + count]) == 0:
out = out + ' '
else:
out = out + '.'
if (x + count + 1) >= len(str):
x += 1
y = 0
while y < (indent - x):
out += ' '
y += 1
x += 1
print out
count += indent
print
# given a hex string like '035344534c30384780e2b9f64d00f501'
# turn it back into binary
# binascii.a2b_hex might be a better choice
def unhex(s):
o = ''
for (a, b) in zip(s[0::2], s[1::2]):
o = o + chr(int(a+b, 16))
return o
if __name__ == "__main__":
s = "lksdhfkahsdflakhjsdflahfalsdfakhfdw98345\000\001\006\077$%$^%$%"
hexdump(s, "%% ")
hexdump(s, '', 90)
for i in [1, 0x24, 0x23, 255, 192, 7, 42, 128, 42 + 128, 342, 65537, 0xfffffffff7b0b000]:
print "%20d %65s" % (i, tobin(i))
s = '035344534c30384780e2b9f64d00f501'
print s
hexdump(unhex(s))