小花朵朵

导航

python 进制转换

  1. int(x [,base ])         将x转换为一个整数    
  2. long(x [,base ])        将x转换为一个长整数    
  3. float(x )               将x转换到一个浮点数    
  4. complex(real [,imag ])  创建一个复数    
  5. str(x )                 将对象 x 转换为字符串    
  6. repr(x )                将对象 x 转换为表达式字符串    
  7. eval(str )              用来计算在字符串中的有效Python表达式,并返回一个对象    
  8. tuple(s )               将序列 s 转换为一个元组    
  9. list(s )                将序列 s 转换为一个列表    
  10. chr(x )                 将一个整数转换为一个字符    
  11. unichr(x )              将一个整数转换为Unicode字符    
  12. ord(x )                 将一个字符转换为它的整数值    
  13. hex(x )                 将一个整数转换为一个十六进制字符串    
  14. oct(x )                 将一个整数转换为一个八进制字符串 

 

 

1. int()

>>> int('12', 16)
18
>>> 
>>> 
>>> int('11', 8)
9
>>> 
>>> 
>>> int(12.3, 8)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: int() can't convert non-string with explicit base
>>> int(12.3)
12

2. str

>>> str(1234234235)
'1234234235'
>>> str(0x18)
'24'
>>> 

 

3. chr() VS ord()

>>> chr(67)
'C'
>>> ord('C')
67
>>> 

 

4. hex(), oct(), bin()

>>> hex(12)
'0xc'
>>> oct(12)
'0o14'
>>> bin(12)
'0b1100'
>>> 

 

posted on 2016-11-10 12:34  小花朵朵  阅读(223)  评论(0)    收藏  举报