You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.0 KiB

import struct
def int_list_to_bytes(int_list):
'''
int列表转换为字节流
'''
byte_stream = b''
for num in int_list:
byte_stream += struct.pack('>H', num)
return byte_stream
def bytes_to_int_list(byte_stream):
'''
字节流转换为int列表
'''
int_list = []
for i in range(0, byte_stream.__len__(), 2):
int_list.append(struct.unpack('>H', byte_stream[i:i+2])[0])
return int_list
def get_bit(data: int, bit: int) -> int:
'''
获取某比特的值
'''
if data & (1 << bit):
return 1
else:
return 0
def get_bit_bool(data: int, bit: int) -> bool:
'''
获取某比特的值并转换为布尔值
'''
return bool(get_bit(data, bit))
def set_bit(number: int, pos: int, value: int):
'''
修改某比特的值并返回新值
'''
new_number = None
if value == 1:
new_number = number | (1 << pos)
elif value == 0:
new_number = number & ~(1 << pos)
return new_number