I want to get a python solution for this problem:
e.g.
integer 1 -> string "0x00000001"
integer 64 -> string "0x00000040"
integer 3652458 -> string "0x0037BB6A"
开发者_StackOverflow中文版The string size will not be change if number is in range(0, 2**32)
.
Try this:
'0x%08X' % 3652458
or (with Python 2.6 and newer)
'0x{0:08X}'.format(3652458)
both return:
'0x0037BB6A'
Since Python 3.6 you can use f-strings:
formatted = f"0x{3652458:08X}"
f-strings allow you to put values in a string, where they are actually supposed to go, instead of having to juggle a number of format parameters in your head:
value = 3652458
print(f"Your formatted value is 0x{value:08x}")
精彩评论