diff options
Diffstat (limited to 'hexdump.py')
-rw-r--r-- | hexdump.py | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/hexdump.py b/hexdump.py new file mode 100644 index 0000000..c119b16 --- /dev/null +++ b/hexdump.py | |||
@@ -0,0 +1,27 @@ | |||
1 | import struct | ||
2 | from binascii import b2a_hex | ||
3 | """ | ||
4 | Simple hexdump, 16 bytes per line with offset. | ||
5 | """ | ||
6 | |||
7 | def ashex(line): | ||
8 | return " ".join("%02x" % _ for _ in line) | ||
9 | def aschr(b): | ||
10 | if 32<=b<0x7f: | ||
11 | return "%c" % b | ||
12 | elif 0x80<=b<=0xff: | ||
13 | try: | ||
14 | c = struct.pack("<B", b).decode('cp1251') | ||
15 | if c: | ||
16 | return c | ||
17 | except: | ||
18 | pass | ||
19 | return "." | ||
20 | def asasc(line): | ||
21 | return "".join(aschr(_) for _ in line) | ||
22 | def hexdump(ofs, data): | ||
23 | for o in range(0, len(data), 16): | ||
24 | print("%08x: %-47s %s" % (o+ofs, ashex(data[o:o+16]), asasc(data[o:o+16]))) | ||
25 | |||
26 | def tohex(data): | ||
27 | return b2a_hex(data).decode('ascii') | ||