python 核心數
Python hex() function is used to convert an integer to a lowercase hexadecimal string prefixed with “0x”.
Python hex()函數用于將整數轉換為以“ 0x”為前綴的小寫十六進制字符串。
We can also pass an object to hex() function, in that case the object must have __index__()
function defined that returns integer.
python進制、 我們還可以將一個對象傳遞給hex()函數,在這種情況下,該對象必須定義返回整數的__index__()
函數。
The input integer argument can be in any base such as binary, octal etc. Python will take care of converting them to hexadecimal format.
輸入的整數參數可以是任何基數,例如二進制,八進制等。Python將負責將它們轉換為十六進制格式。
Let’s look into some simple examples of converting integer to hexadecimal number.
讓我們看一些將整數轉換為十六進制數的簡單示例。
print(hex(255)) # decimal
print(hex(0b111)) # binary
print(hex(0o77)) # octal
print(hex(0XFF)) # hexadecimal
python中eval函數作用,Output:
輸出:
0xff
0x7
0x3f
0xff
Let’s create a custom class and define __index__() function so that we can use hex() function with it.
讓我們創建一個自定義類并定義__index __()函數,以便我們可以對其使用hex()函數。
class Data:id = 0def __index__(self):print('__index__ function called')return self.idd = Data()
d.id = 100print(hex(d))
Output:
python編程? 輸出:
__index__ function called
0x64
Reference: Official Documentation
參考: 官方文檔
翻譯自: https://www.journaldev.com/22902/python-hex
python 核心數
版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。
工作时间:8:00-18:00
客服电话
电子邮件
admin@qq.com
扫码二维码
获取最新动态