TypeError: '<=' not supported between instances of 'str' and 'int'

Question:

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’"

Asked By: Mohammed Farttoos

||

Answers:

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])
Answered By: PandaBlue

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
Answered By: rzz
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.