例如:将 int型数值装换成要求宽度的hex型字符串
def to_hex_string(number: int, byte_cnt):
"""
convert the number to hex string which length is byte_cnt bytes
:param number: the vaulee need be converted, eg 3
:param byte_cnt: byte counters, e.g. 2
:return: converted hex string, e.g 0x0003
"""
hex_string = "{0:#0{1}x}".format(number, byte_cnt * 2 + 2)
hex_string = hex_string[:2] + hex_string[2:].swapcase()
return hex_string
参考官网的 PEP-3101 https://www.python.org/dev/peps/pep-3101/
Standard Format Specifiers is :
[[fill] align] [sign] [#] [0] [minimumwidth] [,precision][type]