-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathHexDump.py
33 lines (32 loc) · 941 Bytes
/
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
def HexByte(byte):
byte = ord(byte)
digits = "0123456789ABCDEF"
return digits[byte >> 4] + digits[byte & 0x0F]
def HexDump(bytes, offset=0, bytesPerLine=32, offsetFormat="%08X: ", verbose=False):
printable = "." * 32 + "".join(map(chr, range(32,127))) + "." * 129
if offsetFormat is None or offset is None:
offsetFormat = ""
length = len(bytes)
index = 0
lastLine = ""
skipping = False
while index < length:
if "%" in offsetFormat:
offStr = offsetFormat % offset
else:
offStr = ""
line = bytes[index:index+bytesPerLine]
if line == lastLine and not verbose:
if not skipping:
print "*"
skipping = True
else:
hex = " ".join(map(HexByte, line))
ascii = line.translate(printable)
print "%s%-*s |%s|" % (offStr, 3*bytesPerLine, hex, ascii)
lastLine = line
skipping = False
index += bytesPerLine
offset += bytesPerLine
if skipping and ("%" in offsetFormat):
print offsetFormat % offset