在下面的代码中,int()对这两个参数做了什么:
if (i=='0X0F'):
stat = int(log[i+1],16)
Run Code Online (Sandbox Code Playgroud)
Joh*_*ica 11
class int(object)
| int(x=0) -> int or long
| int(x, base=10) -> int or long
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is floating point, the conversion truncates towards zero.
| If x is outside the integer range, the function returns a long instead.
|
| If x is not a number or if base is given, then x must be a string or
| Unicode object representing an integer literal in the given base. The
| literal can be preceded by '+' or '-' and be surrounded by whitespace.
| The base defaults to 10. Valid bases are 0 and 2-36. Base 0 means to
| interpret the base from the string as an integer literal.
Run Code Online (Sandbox Code Playgroud)
第二个参数告诉int
输入字符串的基数。来自帮助:
class int(object)
| int(x=0) -> integer
| int(x, base=10) -> integer
|
| Convert a number or string to an integer, or return 0 if no arguments
| are given. If x is a number, return x.__int__(). For floating point
| numbers, this truncates towards zero.
|
| If x is not a number or if base is given, then x must be a string,
| bytes, or bytearray instance representing an integer literal in the
| given base. The literal can be preceded by '+' or '-' and be surrounded
| by whitespace. The base defaults to 10. Valid bases are 0 and 2-36.
| Base 0 means to interpret the base from the string as an integer literal.
Run Code Online (Sandbox Code Playgroud)
因此,如果您这样做int(S, B)
,它会显示“convert” S
,这是一个数字的字符串表示形式B
:
In [63]: int('10', 2)
Out[63]: 2
In [64]: int('10', 3)
Out[64]: 3
Run Code Online (Sandbox Code Playgroud)
现在,如果B
大于 10,则 python 假定下一个数字序列来自ABCD...
。因此:
In [65]: int("A", 11)
Out[65]: 10
Run Code Online (Sandbox Code Playgroud)