Rag*_*gav 4 python number-formatting python-2.7
我正在将十六进制数据转换为十进制,范围从00到FF
hex_data = "FF"
int("0x" + hex_data , 16)
Run Code Online (Sandbox Code Playgroud)
返回255,但是当我将0作为hexdata时,它给出0 wheras我需要它作为000
怎么做
你需要格式化它:
hex_data = "FF"
number = int("0x" + hex_data, 16)
print '%03d' % number # either this
print '{:03d}'.format(number) # or this (Python >= 2.6)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
3332 次 |
| 最近记录: |