forked from philippechataignon/smrt
-
Notifications
You must be signed in to change notification settings - Fork 1
/
binary.py
45 lines (37 loc) · 913 Bytes
/
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
#!/usr/bin/env python3
SEP = ","
def ports2list(ports):
if ports is None:
l = []
else:
try:
l = [int(x) for x in ports.split(SEP)]
except ValueError:
l = []
return l
def ports2byte(ports):
out = 0
l = ports2list(ports)
if l == []:
out = 0
else:
for i in l:
out |= (1 << (int(i) - 1))
return out
def byte2ports(byte):
out = []
for i in range(32):
if byte % 2:
out.append(str(i + 1))
byte >>= 1
return SEP.join(out)
def mac_to_bytes(mac):
return bytes(int(byte, 16) for byte in mac.split(':'))
def mac_to_str(mac):
return ':'.join(format(s, '02x') for s in mac)
if __name__ == '__main__':
a = ports2byte("1,2,5,6,8,12,15")
print(a, byte2ports(a))
print(ports2list("1,2"))
m = mac_to_bytes("ba:ff:ee:ff:ac:ee")
print(mac_to_str(m))