從上述報錯,可知int類參數必須是 str, bytes, bytearray, int, float 五種類型
int() 可接受兩個參數 base默認是10進制
- 無參 -> zero
- 第一參數為 number類型
TypeError 不能帶明確的base, 即base就是默認10進制
可將float轉為int
- 第一參數為 str bytes bytearray 非number類型
則其必須representing an interger literal in the given base, 即必須是指定base的字面值
base=0 會根據literal智能選擇base 即Python, int前綴 0b 0o 0x - class method
Syntax
int.from_bytes(bytes, byteorder, *, signed=False)
x86為little endian, b=b'\x21\x19' 是我們的literal, 在內存中是 '\x19\x21'存儲的, 故使用 byteorder='big' 可得到字面值, 而如果使用 'little' 則表示 '\x21\x19' 即使內存中實際存儲格式, 其實際值為 '\x19\x21'
signed=False (default) 默認當作 unsigned解析 - int.to_bytes() 實例方法