-
Notifications
You must be signed in to change notification settings - Fork 0
/
myIO.py
127 lines (114 loc) · 3 KB
/
myIO.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
# 2021.10.14
# @yifan
import numpy as np
class myIO():
@staticmethod
def int2chr(val):
val = (int)(val)
assert val >=0 and val < 256, "Value Error!"
return chr(val)
@staticmethod
def chr2int(char):
return (ord)(char)
@staticmethod
def bstr2chr(bst):
assert len(bst) <= 8, "Length Error!"
val = bstr2int(bst)
return int2chr(val)
@staticmethod
def chr2bstr(char):
val = chr2int(char)
bst = int2bstr(val)
return bst
@staticmethod
def int2bstr(val):
bst = "{0:b}".format(val)
while len(bst) < 8:
bst = '0' + bst
return bst
@staticmethod
def bstr2int(bst):
val = 0
while len(bst) < 8:
bst += '0'
for i in range(len(bst)):
if bst[i] == '0':
val = (val<<1)
elif bst[i] == '1':
val = (val<<1) + 1
else:
assert False, "Value Error!"
return val
@staticmethod
def blist2bstr(blist):
bstr = ''
for i in blist:
if i == '0' or i == 0:
bstr += '0'
elif i == '1' or i == 1:
bstr += '1'
else:
assert False, "Value Error!"
return bstr
@staticmethod
def bstr2blist(bstr):
blist = []
for i in bstr:
if i == '0':
blist.append(0)
elif i == '1':
blist.append(1)
else:
assert False, "Value Error!"
return blist
def int2bits(integer, lenth, is_uint=True, return_string=False):
integer = (int)(integer)
if is_uint == False:
integer += pow(2, lenth-1)
if integer >= pow(2, lenth):
print("Warning dymanic range too large", integer, lenth)
if integer >= 0:
tmp = bin(integer)[2:].zfill(lenth)
else:
print('ERROR')
assert False, "Dymanic range not correct!"
bools = []
if return_string == True:
return tmp
for i in range(len(tmp)):
if tmp[i] == '0':
bools.append(False)
else:
bools.append(True)
assert (len(bools) == lenth), '<error> lenth not match'
return bools
def bits2int(bits, is_uint=True):
tmp = 0
lenth = len(bits)
for b in bits:
if b == True or b == '1':
tmp = tmp * 2 + 1
else:
tmp = tmp * 2 + 0
if is_uint == False:
tmp -= pow(2, lenth-1)
return (int)(tmp)
def myWrite(b, f):
if type(b) == list:
b = myIO.blist2bstr(b)
for i in range(0, len(b), 8):
st = b[i:i+8]
ch = myIO.bstr2chr(st)
f.write(ch)
if len(b[i:]) > 0:
ch = myIO.bstr2chr(st)
f.write(ch)
def myRead(f, size, typ='bstr'):
bst = ''
for i in range(size):
v = f.read()
if type == 'bstr':
bst += myIO.chr2bstr(f)
if typ == 'blist':
return myIO.bstr2blist(bst)
return bst