-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprint_binary.py
63 lines (47 loc) · 1.23 KB
/
print_binary.py
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
51
52
53
54
55
56
57
58
59
60
61
62
63
def print_binary(data, prnt=True):
def print_line(offset, dec, char, prnt=True):
if len(dec) == 0:
return
line_offset = '%08X: ' % offset
line_dec = ''
line_char = ''
for d in dec:
line_dec += '%02X ' % d
for c in char:
line_char += '%c' % c
pad = 16 - len(dec)
for _ in range(pad):
line_dec += ' '
line_total = line_offset + line_dec + ' ' + line_char
if offset == 0 and prnt:
print('-' * len(line_total))
if prnt:
print(line_total)
return
return line_total
char = []
dec = []
result = []
offset = 0
i = 0
for c in data:
d = ord(c)
dec.append(d)
if d >= 32 and d < 127:
char.append(c)
else:
char.append('.')
i += 1
if i == 16:
result.append(print_line(offset, dec, char, prnt=prnt))
offset += 16
i = 0
char = []
dec = []
# print last line
last_line = print_line(offset, dec, char, prnt=prnt)
if last_line:
result.append(last_line)
if prnt:
return
return result