开发者

Python - how to convert int to string represent a 32bit Hex number

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}")
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜