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.

76 lines
1.5 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
# float value to 2 int list
def float2intList(dataf: float):
parts = struct.pack('>f', float(dataf))
output = parts[2:] + parts[:2]
vl_output = bytes_to_int_list(output)
return vl_output
# 2 int list to float value
def intList2Float(intList = []):
# print("intList: ", intList)
r_drawer = int_list_to_bytes(intList)
routput = r_drawer[2:] + r_drawer[:2]
floatValue = struct.unpack('>f', routput)
return round(floatValue[0], 3)