Python整数到带填充的十六进制字符串 [英] Python integer to hex string with padding
本文介绍了Python整数到带填充的十六进制字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
考虑一个整数2.我想将其转换为十六进制字符串'0x02'.通过使用python的内置函数hex()
,我可以获得不适合我的代码的'0x2'.谁能告诉我如何以一种便捷的方式获得我想要的东西?谢谢你.
Consider an integer 2. I want to convert it into hex string '0x02'. By using python's built-in function hex()
, I can get '0x2' which is not suitable for my code. Can anyone show me how to get what I want in a convenient way? Thank you.
推荐答案
integer = 2
hex_string = '0x{:02x}'.format(integer)
请参见 pep 3101 ,尤其是标准格式说明符了解更多信息.
See pep 3101, especially Standard Format Specifiers for more info.
这篇关于Python整数到带填充的十六进制字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文