-
Notifications
You must be signed in to change notification settings - Fork 0
/
Header.py
68 lines (55 loc) · 1.82 KB
/
Header.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
# kodowanie i dekodowanie systemu binarnego
class Header:
# konstruktor
def __init__(self, op, ans, sesID, number):
self.all = []
self.all.append(op) # all[0]; 6 bits
self.all.append(ans) # all[1]; 3 bits
self.all.append(sesID) # all[2]; 3 bits
self.all.append(number) # all[3]; all[3] bytes
# kodowanie na binarny system
def getHeader(self):
arrayBH = bytearray(6)
arrayBH[0] += self.all[0]
arrayBH[0] <<= 2
arrayBH[0] += ((self.all[1] & 6) >> 1)
arrayBH[1] += self.all[1] & 1
arrayBH[1] <<= 3
arrayBH[1] += self.all[2]
arrayBH[1] <<= 4
arrayBH[1] += ((self.all[3] & 4026531840) >> 28)
tmpINT = (self.all[3]<<4)
arrayBH[2] += ((tmpINT & 0xFF000000) >> 24)
arrayBH[3] += ((tmpINT & 0xFF0000) >> 16)
arrayBH[4] += ((tmpINT & 0xFF00) >> 8)
arrayBH[5] += (tmpINT & 0xF0)
return arrayBH
# dekodowanie z binarnego systemu
def setHeader(self, rec):
data = int.from_bytes(rec, 'big')
bitShift = ((2 ** 4), (2 ** 36), (2 ** 39), (2 ** 42), (2 ** 48))
# operation
tmp = data % bitShift[4]
op = int(tmp/bitShift[3])
# answer
tmp = data % bitShift[3]
ans = int(tmp / bitShift[2])
# id
tmp = data % bitShift[2]
id = int(tmp / bitShift[1])
# number
tmp = data % bitShift[1]
number = int(tmp / bitShift[0])
self.all[0] = op
self.all[1] = ans
self.all[2] = id
self.all[3] = number
# funkcje wyciagajace dane z dekodowanej wiadomosci
def getOp(self):
return self.all[0]
def getAns(self):
return self.all[1]
def getSesID(self):
return self.all[2]
def getNumber(self):
return self.all[3]