0

I have output

New_MS= 102311144

I need to make this value as input by read each two number and add 0x to be hexadecimal, if the last number is just one number then should added 0 to end. As like in below

New_MS= (0x10, 0x23, 0x11, 0x14, 0x40)

Any idea how to do this

2 Answers2

1

Transform to string, then split every second character (see here: Split string every nth character? ).

Then left justify your parts:

New_MS = 102311144

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]
print(f"({','.join(ms_txt_list)})")
PandaBlue
  • 331
  • 1
  • 6
  • Thanks for answer, its near from what I need, but output is ['0x10', '0x23', '0x11', '0x14', '0x40'], I need it New_MS= (0x10, 0x23, 0x11, 0x14, 0x40) exactly same Brackets () and no comma '' – Mohammed Farttoos Dec 23 '22 at 16:00
0

I know we shouldn't provide answers without having OP show his work, but it's almost christmas. Here is my shot:

New_MS= 102311144

s = f"{New_MS}"
start = 0
t = ()
while s[start:start+2]:
    chunk = s[start:start+2]
    if len(chunk) == 1:
        chunk += "0"

    t += (int(chunk, 16),)

    start += 2

print(t)
Bart Friederichs
  • 33,050
  • 15
  • 95
  • 195