天天看點

python hex() oct() bin() 内置函數

說明:

>>> help(hex)
Help on built-in function hex in module __builtin__:
hex(...)
    hex(number) -> string
    Return the hexadecimal representation of an integer or long integer.
>>> help(oct)
Help on built-in function oct in module __builtin__:
oct(...)
    oct(number) -> string
    Return the octal representation of an integer or long integer.
>>> help(bin)
Help on built-in function bin in module __builtin__:
bin(...)
    bin(number) -> string
    Return the binary representation of an integer or long integer.
>>>
           

示例:

print hex(20),hex(-20) #轉換成十六進制
print oct(20),oct(-20) #轉換成八進制
print bin(20),bin(-20) #轉換成二進制
           

結果:

0x14 -0x14

024 -024

0b10100 -0b10100