summaryrefslogtreecommitdiff
path: root/hexdump.py
diff options
context:
space:
mode:
authoritsme <itsme@xs4all.nl>2021-07-06 19:26:42 +0200
committeritsme <itsme@xs4all.nl>2021-07-06 19:26:42 +0200
commita9886b9d52c3bce0a4b58805b5597efccc55225a (patch)
tree4133e30e57109385ade3f756970058fe1edac255 /hexdump.py
initial commit
Diffstat (limited to 'hexdump.py')
-rw-r--r--hexdump.py27
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 @@
1import struct
2from binascii import b2a_hex
3"""
4Simple hexdump, 16 bytes per line with offset.
5"""
6
7def ashex(line):
8 return " ".join("%02x" % _ for _ in line)
9def 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 "."
20def asasc(line):
21 return "".join(aschr(_) for _ in line)
22def 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
26def tohex(data):
27 return b2a_hex(data).decode('ascii')