50 lines
1.4 KiB
Python
50 lines
1.4 KiB
Python
#-*-coding: utf-8 -*-
|
|
|
|
def InsertBlock(hex_str):
|
|
if type(hex_str) != type("12"):
|
|
print("hex_str is not 'str' type!")
|
|
return None
|
|
else:
|
|
if len(hex_str)%2 != 0:
|
|
hex_str += '0'
|
|
print("add '0' at end,amended: ", end="")
|
|
print(hex_str)
|
|
hex_str_len = len(hex_str)
|
|
# print(hex_str_len)
|
|
hex_str_list = ["".join(hex_str[2*i:2*i+2]) for i in range(hex_str_len//2)]
|
|
# print(hex_str_list)
|
|
return " ".join(hex_str_list)
|
|
|
|
def ToHexStr(data):
|
|
if type(data) == type([1,]):
|
|
bytes_data = ToBytes(data)
|
|
return bytes_data.hex()
|
|
elif type(data) == type(b'\x00'):
|
|
return data.hex()
|
|
else:
|
|
print("only 'list' or 'bytes' is valid!")
|
|
return None
|
|
|
|
def ToIntList(data):
|
|
if type(data) == type('12'):
|
|
bytes_data = ToBytes(data)
|
|
return list(bytes_data)
|
|
elif type(data) == type(b'\x00'):
|
|
return list(data)
|
|
else:
|
|
print("only 'str' or 'bytes' is valid!")
|
|
return None
|
|
|
|
def ToBytes(data):
|
|
if type(data) == type('12'):
|
|
if len(data)%2 != 0:
|
|
data += '0'
|
|
print("add '0' at end,amended: ", end="")
|
|
print(data)
|
|
return bytes().fromhex(data)
|
|
elif type(data) == type([1,]):
|
|
return bytes(data)
|
|
else:
|
|
print("only 'str' or 'list' is valid!")
|
|
return None
|