Why when put input value such as hexadecimal values like in below
if __name__ == '__main__':
p = (0x41, 0x31, 0x31, 0x37)
it take it as input without error but if I get p from output such as (answer from my previous question)
New_MS = 41313137
str_ms = str(New_MS)
n = 2
split_str_ms = [str_ms[i : i n] for i in range(0, len(str_ms), n)]
ms_txt_list = [f"0x{d.ljust(2, '0')}" for d in split_str_ms]
p=(f"({','.join(ms_txt_list)})")
where the output is should be same
(0x41,0x31,0x31,0x37)
But I got error `"TypeError: '<=' not supported between instances of 'str' and 'int'"
CodePudding user response:
if your input 41313137
is in fact a text representation of a list of bytes, the transformation to "print out" a tuple of byte is unecessary.
is this code doing what you need?
New_MS = 41313137
str_ms = str(New_MS)
if len(str_ms) % 2 == 1:
str_ms = str_ms '0'
byte_ms = bytearray.fromhex(str_ms)
print([hex(b) for b in byte_ms])
CodePudding user response:
You can try to convert 'p' to a tuple of integers:
import ast
if __name__ == '__main__':
p = (0x41, 0x31, 0x31, 0x37)
New_MS = 41313137
str_ms = str(New_MS)
n = 2
split_str_ms = [str_ms[i : i n] for i in range(0, len(str_ms), n)]
ms_txt_list = [f"0x{d.ljust(2, '0')}" for d in split_str_ms]
p = f"({','.join(ms_txt_list)})"
p = ast.literal_eval(p) # here you convert