我發現在python一些奇怪的語法:用於在Python中將字符串轉換爲int的代碼中的第二個參數是什麼?
int(array[0], 32)
array[0]
是一些字符串,但究竟什麼是第二個參數?有人可以解釋這個int()
語法,它是功能還是鑄造?
我發現在python一些奇怪的語法:用於在Python中將字符串轉換爲int的代碼中的第二個參數是什麼?
int(array[0], 32)
array[0]
是一些字符串,但究竟什麼是第二個參數?有人可以解釋這個int()
語法,它是功能還是鑄造?
Help on class int in module __builtin__:
class int(object)
| int(x[, base]) -> integer
|
| Convert a string or number to an integer, if possible. A floating point
| argument will be truncated towards zero (this does not include a string
| representation of a floating point number!) When converting a string, use
| the optional base. It is an error to supply a base when converting a
| non-string. If base is zero, the proper base is guessed based on the
| string content. If the argument is outside the integer range a
| long object will be returned instead.
所以這是您正在解析,以數字範圍內的base32串從0
.. 9
和a
.. v
。
http://docs.python.org/library/functions.html#int
int([x[, base]])
第二個參數是在其中轉換是要進行的鹼:
help(int)
class int(object)
| int(x[, base]) -> integer
....
這可不是件,第二個參數指定了數記基地:http://docs.python.org/library/functions.html#int
下一次你遇到類似的問題 試試這個:二進制數的
>>> help (int)
打印十進制值作爲字符串提供
>>> int('1010101011',2)
683
打印作爲字符串提供的十進制數的十進制值
>>> int('1010101011',10)
1010101011
>>>
失敗了,因爲我們沒有在二進制
>>> int('120000000',2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: invalid literal for int() with base 2: '120000000'
有符號 「2」