1def HexByte(byte):
2	byte = ord(byte)
3	digits = "0123456789ABCDEF"
4	return digits[byte >> 4] + digits[byte & 0x0F]
5
6def HexDump(bytes, offset=0, bytesPerLine=32, offsetFormat="%08X: ", verbose=False):
7	printable = "." * 32 + "".join(map(chr, range(32,127))) + "." * 129
8	if offsetFormat is None or offset is None:
9		offsetFormat = ""
10	length = len(bytes)
11	index = 0
12	lastLine = ""
13	skipping = False
14	while index < length:
15		if "%" in offsetFormat:
16			offStr = offsetFormat % offset
17		else:
18			offStr = ""
19		line = bytes[index:index+bytesPerLine]
20		if line == lastLine and not verbose:
21			if not skipping:
22				print "*"
23				skipping = True
24		else:
25			hex = " ".join(map(HexByte, line))
26			ascii = line.translate(printable)
27			print "%s%-*s |%s|" % (offStr, 3*bytesPerLine, hex, ascii)
28			lastLine = line
29			skipping = False
30		index += bytesPerLine
31		offset += bytesPerLine
32	if skipping and ("%" in offsetFormat):
33		print offsetFormat % offset
34