42 lines
1.0 KiB
Python
42 lines
1.0 KiB
Python
class BCCCalculator:
|
||
"""
|
||
Класс для работы с BCC
|
||
"""
|
||
|
||
@staticmethod
|
||
def xor_bcc(data):
|
||
"""XOR BCC"""
|
||
if isinstance(data, str):
|
||
data = data.encode('utf-8')
|
||
elif isinstance(data, list):
|
||
data = bytes(data)
|
||
|
||
bcc = 0
|
||
for byte in data:
|
||
bcc ^= byte
|
||
return bcc
|
||
|
||
@staticmethod
|
||
def sum_bcc(data):
|
||
"""Суммирование BCC"""
|
||
if isinstance(data, str):
|
||
data = data.encode('utf-8')
|
||
elif isinstance(data, list):
|
||
data = bytes(data)
|
||
|
||
total = sum(data)
|
||
return total & 0xFF
|
||
|
||
@staticmethod
|
||
def verify_bcc(data_with_bcc):
|
||
"""
|
||
Проверка BCC в данных
|
||
"""
|
||
if len(data_with_bcc) < 2:
|
||
return False
|
||
|
||
data = data_with_bcc[:-1]
|
||
expected_bcc = data_with_bcc[-1]
|
||
calculated_bcc = BCCCalculator.xor_bcc(data)
|
||
|
||
return expected_bcc == calculated_bcc |