[go: up one dir, main page]

Menu

[r80]: / hexutil.py  Maximize  Restore  History

Download this file

35 lines (29 with data), 892 Bytes

 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
#!/usr/bin/env python
class Group:
def __init__(self, l, size):
self.size=size
self.l = l
def __getitem__(self, group):
idx = group * self.size
if idx > len(self.l):
raise IndexError("Out of range")
return self.l[idx:idx+self.size]
def safe_prn(str):
import string
ostr = ''
for c in str:
if c in string.printable and c not in "\t\n\r\x0b\x0c":
ostr += c
else:
ostr += '.'
return ostr
def hd(str, offset=0, header=True):
ostr = ''
if header: ostr += " 0 1 2 3 4 5 6 7 8 9 A B C D E F\n"
for i in range(0,len(str), 16):
hexpart = ' '.join(Group(str[i:i+16].encode('hex'), 2))
ostr += "%08X: %-48s %-16s\n" % (offset+i, hexpart, safe_prn(str[i:i+16]))
return ostr
def decode_hex(str):
str = ''.join(str.split())
return str.decode('hex')