调整脚本结构, 清理冗余代码;
This commit is contained in:
31
source/tools/ByteConv.py
Normal file
31
source/tools/ByteConv.py
Normal file
@@ -0,0 +1,31 @@
|
||||
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, len) -> str:
|
||||
""" Hex字符表示 """
|
||||
data %= 2 ** (4 * len)
|
||||
result = "0" * len + hex(data)[2:]
|
||||
return "0x" + result[-len:].upper()
|
||||
Reference in New Issue
Block a user