数字变为字符串 str()
字符串变为数字 string.atoi(s,[,base]) //base为进制基数
浮点数转换 string.atof(s)
需求:
需要把一个字符(ASCII或Unicode)转换为数字编码
,或者反过来转换.
讨论:
对于ASCII字符,可以使用内建的ord和chr方法实现需求:
>>> chr(97)
'a'
>>> ord('a')
97
对于Unicode字符,需要使用ord和repr,获得unicode字符的方法,使用unichr:
>>> print ord(u'\u2020')
8224
>>> print repr(unichr(8224))
u'\u2020'
相关说明:
下面是python中对这几个方法的简单说明:
ord(...)
ord(c) -> integer
Return the integer ordinal of a one-character string.
chr(...)
chr(i) -> character
Return a string of one character with ordinal i; 0 <= i < 256.
repr(...)
repr(object) -> string
Return the canonical string representation of the object.
For most object types, eval(repr(object)) == object.
unichr(...)
unichr(i) -> Unicode character
Return a Unicode string of one character with ordinal i; 0 <= i <= 0x10ffff.