This repository was archived by the owner on Jul 13, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhexdump.py
More file actions
50 lines (40 loc) · 1.23 KB
/
Copy pathhexdump.py
File metadata and controls
50 lines (40 loc) · 1.23 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import string
bytes_per_line = 16
bytes_per_block = 4
nonprintable = '.'
addr_format = '%8x : '
byte_format = '%2x '
byte_fmt_len = 3
def hexdump(raw_string, start_at=0):
i = start_at
s = ''
asc = ''
# from the first line
initial_offset = (start_at % bytes_per_line)
if initial_offset:
offset = byte_fmt_len * initial_offset
offset += initial_offset / bytes_per_block
if initial_offset % bytes_per_block:
offset += 1
s = ' ' * offset
asc = ' ' * initial_offset
for b in raw_string:
if 0 == i % bytes_per_block:
s += ' '
s += '%02x ' % ord(b)
if b in string.printable and ord(b) >= 0x10:
asc += b
else:
asc += nonprintable
i += 1
if 0 == i % bytes_per_line and i > start_at:
s += '| ' + asc
print (addr_format % (i - bytes_per_line)) + s
s = ''
asc = ''
finish_offset = i % bytes_per_line
if finish_offset:
offset = byte_fmt_len * (bytes_per_line - finish_offset)
offset += (bytes_per_line - finish_offset) / bytes_per_block
s += ' ' * offset
print (addr_format % (i - finish_offset)) + s + '| ' + asc