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 Answers 2

2

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)})")
Sign up to request clarification or add additional context in comments.

1 Comment

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 ''
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)

2 Comments

OK it good but the output is (16, 35, 17, 20, 64) not (0x10, 0x23, 0x11, 0x14, 0x40) what I asked for
Your request shows not a tuple of strings, but a tuple of integers. These values are the same.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.