由于在python2中的命令encode和decode已经不在适用python3
def string2number(str): """Convert a string to a number Input: string(big-endian) Output: long or integer """ return int(str.encode(‘hex‘),16) mypresent.py", line 36, in string2number return int(str.encode(‘hex‘),16) LookupError: ‘hex‘ is not a text encoding; use codecs.encode() to handle arbitrary codecs
所以特地学习python中的数字表示方法,
binary二进制(0b101)、
octal八进制(0o74125314)、
decimal十进制(1223)、
hexadecimal十六进制(0xff)
而且可以为加下来做分组加密打下coding基础
‘‘‘
input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数
output -- a string: 输出为以0b开头的二进制字符串
‘‘‘
example:
>>> bin(0x11) ‘0b10001‘ >>> bin(0b1010111) ‘0b1010111‘ >>> bin(0o1234567) ‘0b1010011100101110111‘ >>> bin(1234567) ‘0b100101101011010000111‘ >>> bin(0x1234f567ff) ‘0b1001000110100111101010110011111111111‘ >>> bin(bin(123)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘str‘ object cannot be interpreted as an integer >>>
‘‘‘
input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数
output -- a string: 输出为以0o开头的二进制字符串
‘‘‘
example:
>>> oct(0b1111) ‘0o17‘ >>> oct(0o1111) ‘0o1111‘ >>> oct(1111) ‘0o2127‘ >>> oct(0xff1111) ‘0o77610421‘ >>> oct(oct(0xff1111)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘str‘ object cannot be interpreted as an integer >>>
‘‘‘
input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数
output -- a string: 输出为以0x开头的二进制字符串
‘‘‘
example:
>>> hex(0b1111) ‘0xf‘ >>> hex(0o1111) ‘0x249‘ >>> hex(1111) ‘0x457‘ >>> hex(0x1111) ‘0x1111‘ >>> hex(hex(0x1111)) Traceback (most recent call last): File "<stdin>", line 1, in <module> TypeError: ‘str‘ object cannot be interpreted as an integer >>>
‘‘‘
input -- a number: 输入参数可以为二进制数、八进制数、十进制数、十六进制数、浮点数
output -- a string: 输出整数,浮点数向下取整
‘‘‘
构造函数如下:
int(x=0) --> integer
int(x, base=10) --> integer 给定了参数base(取0,2-36) x必须是字符串形式、bytes
example:
>>> int(‘ffff‘,16) 65535 >>> int(‘ffff‘,8) Traceback (most recent call last): File "<stdin>", line 1, in <module> ValueError: invalid literal for int() with base 8: ‘ffff‘ >>> int(‘ffff‘,36) 719835 >>> int(b‘ffff‘,18) 92625 >>> int(b‘ffff‘,16) 65535 >>>
原文:https://www.cnblogs.com/PiaYie/p/13860723.html