字符串转换成整形
import string
a="12345"
#转换成int
string.atoi(a)
#也可以,不用区分int和long。
int(a)
#不过如果你的数字太长 还是转换成 long
string.atol()
二进制、十六进制与十进制之间的转换
- def Binary2Decimal(bin_num):
- """ Return the decimal representation of bin_num
- This actually uses the built-in int() function,
- but is wrapped in a function for consistency """
- return int(bin_num, 2)
- def hex2Decimal(bin_num):
- return int(bin_num, 16)
- def Decimal2Binary(dec_num):
- """ Return the binary representation of dec_num """
- if dec_num == 0: return ''
- head, tail = divmod(dec_num, 2)
- return Decimal2Binary(head) + str(tail)