How adding 0x to output values to be as hexadecimal input value
Question:
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
Answers:
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)})")
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)
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
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)})")
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)