修复十六进制数的大小 Python
Make Fix Size of Hex Number Python
我想让我的十六进制数有 2 个字节大小。我该怎么做?例如,如果在 C 中我们可以使 %02f
。我如何在 Python2.7 中做到这一点?
谢谢
你可以用类似的方式格式化它。只需使用 "%02x" % (the_number,)
在 python 中,十六进制的使用并不常见。因此,它通常表示为字符串或整数,即:
print type(0x50),0x50
#returns <type 'int'> 80
print type(hex(80),hex(80)
#returns <type 'str'>, '0x50'
了解这一点的您,可以通过以下方式利用此 属性:
myhex_int=0x5 #integer with value 5
myhex_str='%02d'%myhex_int #string '05',use '0x02d' if you prefer this
#to return it to an integer again:
myhex2=int(myhex_str,16) #set base to 16
#integer with value 5
我尝试这样使用:"{0:03x}".format(number)
并且有效
我想让我的十六进制数有 2 个字节大小。我该怎么做?例如,如果在 C 中我们可以使 %02f
。我如何在 Python2.7 中做到这一点?
谢谢
你可以用类似的方式格式化它。只需使用 "%02x" % (the_number,)
在 python 中,十六进制的使用并不常见。因此,它通常表示为字符串或整数,即:
print type(0x50),0x50
#returns <type 'int'> 80
print type(hex(80),hex(80)
#returns <type 'str'>, '0x50'
了解这一点的您,可以通过以下方式利用此 属性:
myhex_int=0x5 #integer with value 5
myhex_str='%02d'%myhex_int #string '05',use '0x02d' if you prefer this
#to return it to an integer again:
myhex2=int(myhex_str,16) #set base to 16
#integer with value 5
我尝试这样使用:"{0:03x}".format(number)
并且有效