新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)Python教程:Pythonhex()
hex()函數(shù)有助于將給定的數(shù)字轉(zhuǎn)換為相應(yīng)的十六進(jìn)制字符串格式。返回的十六進(jìn)制字符串的前綴必須為“0x”。

**hex(number)** #Where number shows a integer number
十六進(jìn)制()參數(shù):
只接受一個參數(shù)。如果我們將一個對象作為參數(shù)傳遞給hex()函數(shù),該對象必須定義返回整數(shù)的 __index__()函數(shù)。
| 參數(shù) | 描述 | 必需/可選 |
|---|---|---|
| 目標(biāo) | int 對象,或者它必須定義一個返回整數(shù)的__index__()方法 | 可選擇的 |
| 整數(shù) | 可以是任何基數(shù),如二進(jìn)制、八進(jìn)制等 | 可選擇的 |
十六進(jìn)制()返回值
要獲得浮點數(shù)的十六進(jìn)制表示,需要使用float.hex()方法。
| 投入 | 返回值 | | 整數(shù) | 十六進(jìn)制格式 | | 漂浮物 | 十六進(jìn)制格式 | | 目標(biāo) | 十六進(jìn)制格式 |
Python 中hex()方法的示例
示例hex()是如何工作的?
number = 435
print(number, 'in hex =', hex(number))
number = 0
print(number, 'in hex =', hex(number))
number = -34
print(number, 'in hex =', hex(number))
returnType = type(hex(number))
print('Return type from hex() is', returnType)
輸出:
435 in hex = 0x1b3
0 in hex = 0x0
-34 in hex = -0x22
Return type from hex() is
示例 2:浮點數(shù)的十六進(jìn)制表示
number = 2.5
print(number, 'in hex =', float.hex(number))
number = 0.0
print(number, 'in hex =', float.hex(number))
number = 10.5
print(number, 'in hex =', float.hex(number))
輸出:
2.5 in hex = 0x1.4000000000000p+1
0.0 in hex = 0x0.0p+0
10.5 in hex = 0x1.5000000000000p+3
示例hex()如何處理對象?
class Data:
id = 0
def __index__(self):
print('__index__ function called')
return self.id
d = Data()
d.id = 100
print(hex(d))
輸出:
__index__ function called
0x64 網(wǎng)頁標(biāo)題:創(chuàng)新互聯(lián)Python教程:Pythonhex()
網(wǎng)頁鏈接:http://www.dlmjj.cn/article/djchihh.html


咨詢
建站咨詢
