-
Notifications
You must be signed in to change notification settings - Fork 33
/
ascii_table.py
49 lines (42 loc) · 1 KB
/
ascii_table.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
from sys import stdout
try:
unichr # Python 2
except NameError:
unichr = chr # Python 3
ASCII = False
NAMES = {
0: 'NUL',
7: 'BEL',
9: 'TAB',
10: 'LF',
13: 'CR',
27: 'ESC',
127: 'DEL',
}
HEADER_WIDTH = 4
WIDTH = 5
LINE = ("+" + "-" * HEADER_WIDTH + ("+" + "-" * WIDTH) * 16 + "+\n")
stdout.write(LINE)
stdout.write("|" + " " * HEADER_WIDTH)
for ligne in range(16):
stdout.write("| -%x " % ligne)
stdout.write("|\n")
stdout.write(LINE)
if ASCII:
LINES = range(8)
else:
LINES = range(16)
for ligne in LINES:
text = "%x-" % ligne
stdout.write("| %s " % text)
for colonne in range(16):
ch = unichr(ligne*16 + colonne)
if ord(ch) in NAMES:
ch = NAMES[ord(ch)]
elif ord(ch) < 32 or ord(ch) in (127, 0xAd) or (0x80 <= ord(ch) <= 0x9F):
ch = '---'
elif ch in '*+-`|':
ch = '\\' + ch
stdout.write("| %s " % ch.center(3).encode('utf-8'))
stdout.write("|\n")
stdout.write(LINE)