2013-07-05 213 views

回答

3

为什么不只是做hex()

>>> testNum = 0xa1b2c3 
>>> hex(testNum) 
    '0xa1b2c3' 
>>> test = hex(testNum) 
>>> isinstance(test, str) 
    True 

hex返回字符串表示。请参阅help(hex)

hex(...) 
    hex(number) -> string 

    Return the hexadecimal representation of an integer or long integer. 
2

使用hex

>>> x = 0xa1b2c3 
>>> hex(x) 
'0xa1b2c3' 

string formatting

>>> "{:#x}".format(x) 
'0xa1b2c3' 

format

>>> format(x, '#x') 
'0xa1b2c3' 
+2

改用'format()'函数; '格式(x,'#x')'。不需要有字符串格式解析的开销。 –

+0

谢谢。看起来你们同时回答同样的问题,所以我接受了他的回答,因为他的声望点远远低于你。干杯 –

相关问题