重构代码.
This commit is contained in:
29
source/device/function/tools/ByteConv.py
Normal file
29
source/device/function/tools/ByteConv.py
Normal file
@@ -0,0 +1,29 @@
|
||||
def trans_list_to_str(data: list) -> str:
|
||||
""" 标准串口字符串表示 """
|
||||
func_trans = lambda x: ('00' + hex(x % 256)[2:])[-2:].upper()
|
||||
return " ".join(map(func_trans, data))
|
||||
|
||||
|
||||
def trans_str_to_list(data: str) -> list:
|
||||
""" 标准串口字符串转换列表 """
|
||||
func_trans = lambda x: int(x, 16)
|
||||
return list(map(func_trans, data.strip().split(" ")))
|
||||
|
||||
|
||||
def conv_int_to_array(num: int, big_end=False):
|
||||
""" 数值转字节数组 """
|
||||
result = [0, 0, 0, 0]
|
||||
if big_end:
|
||||
indexlist = reversed(range(len(result)))
|
||||
else:
|
||||
indexlist = range(len(result))
|
||||
|
||||
for i in indexlist:
|
||||
result[i] = int(num % 0x100)
|
||||
num //= 0x100
|
||||
return result
|
||||
|
||||
|
||||
def display_hex(data:int, length:int=2) -> str:
|
||||
""" Hex字符固定最小长度表示 """
|
||||
return f"0x{data:0{length}X}"
|
||||
Reference in New Issue
Block a user