hex() 函數將整數轉換為相應的十六進製字符串。
用法:
hex(x)
參數:
hex()
函數采用單個參數。
x- 整數(int
對象或它必須定義__index__()
返回整數的方法)
返回:
hex()
函數將整數轉換為字符串形式對應的十六進製數並返回。
返回的十六進製字符串以前綴0x
開頭,表明它是十六進製形式。
示例 1: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 <class 'str'>
如果需要查找浮點數的十六進製表示,則需要使用float.hex()
方法。
示例 2:浮點數的十六進製表示
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
相關用法
- Python hex()用法及代碼示例
- Python help()用法及代碼示例
- Python hashlib.sha3_256()用法及代碼示例
- Python hashlib.sha3_512()用法及代碼示例
- Python hashlib.blake2s()用法及代碼示例
- Python hashlib.blake2b()用法及代碼示例
- Python hasattr()用法及代碼示例
- Python html.escape()用法及代碼示例
- Python statistics harmonic_mean()用法及代碼示例
- Python html.unescape()用法及代碼示例
- Python math hypot()用法及代碼示例
- Python hash()用法及代碼示例
- Python hashlib.sha3_224()用法及代碼示例
- Python hashlib.shake_256()用法及代碼示例
- Python OpenCV haveImageReader()用法及代碼示例
- Python hashlib.shake_128()用法及代碼示例
- Python hashlib.sha3_384()用法及代碼示例
- Python torch.distributed.rpc.rpc_async用法及代碼示例
- Python torch.nn.InstanceNorm3d用法及代碼示例
- Python pandas.arrays.IntervalArray.is_empty用法及代碼示例
注:本文由純淨天空篩選整理自 Python hex()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。