-
Notifications
You must be signed in to change notification settings - Fork 12
/
pptutils.py
134 lines (119 loc) · 2.59 KB
/
pptutils.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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
def hex_to_int(hex):
assert hex.startswith('0x')
hex = hex[2:]
total = 0
for h in hex:
total *= 16
total += '0123456789abcdef'.index(h)
return total
def byte_to_uint(byte):
total = 0
for c in byte:
total *= 2
if c == '1':
total += 1
return total
def byte_to_int(byte):
total = 0
for c in byte:
total *= 2
if c == '1':
total += 1
return total if byte[0] == '0' else total - 2**8
def word_to_int(word):
total = 0
for c in word:
total *= 2
if c == '1':
total += 1
return total if word[0] == '0' else total - 2**16
def dword_to_int(dword):
total = 0
for c in dword:
total *= 2
if c == '1':
total += 1
return total if dword[0] == '0' else total - 2**32
def word_to_uint(word):
total = 0
for c in word:
total *= 2
if c == '1':
total += 1
return total
def dword_to_uint(dword):
total = 0
for c in dword:
total *= 2
if c == '1':
total += 1
return total
def int_to_byte(x):
if x < 0:
x += 2**8
res = ''
for i in range(8):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res
def int_to_word(x):
if x < 0:
x += 2**16
res = ''
for i in range(16):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res
def uint_to_word(x):
res = ''
for i in range(16):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res
def uint_to_dword(x):
res = ''
for i in range(32):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res[:16], res[16:]
def int_to_dword(x):
if x < 0:
x += 2**32
res = ''
for i in range(32):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res[:16], res[16:]
def uint_to_byte(x):
res = ''
for i in range(8):
if x % 2 == 1:
res = '1' + res
else:
res = '0' + res
x = x // 2
return res
def split_on_spaces(s):
parts = s.replace('\t', ' ').split(' ')
parts = [p.strip() for p in parts if p.strip()]
return parts
def condense_spaces(s):
return ' '.join(split_on_spaces(s))
def pad_to_length(s, l):
assert l >= len(s)
return s + ' ' * (l - len(s))