用法:
hex(x)
將整數轉換為以“0x” 為前綴的小寫十六進製字符串。如果
x
不是 Pythonint
對象,則它必須定義一個返回整數的__index__()
方法。一些例子:>>> hex(255) '0xff' >>> hex(-42) '-0x2a'
如果要將整數轉換為帶前綴或不帶前綴的大寫或小寫十六進製字符串,可以使用以下兩種方式之一:
>>> '%#x' % 255, '%x' % 255, '%X' % 255 ('0xff', 'ff', 'FF') >>> format(255, '#x'), format(255, 'x'), format(255, 'X') ('0xff', 'ff', 'FF') >>> f'{255:#x}', f'{255:x}', f'{255:X}' ('0xff', 'ff', 'FF')
另請參閱
format()
了解更多信息。另請參閱
int()
,以使用 16 為底將十六進製字符串轉換為整數。注意
要獲取浮點數的十六進製字符串表示,請使用
float.hex()
方法。
相關用法
- Python hex()用法及代碼示例
- Python help()用法及代碼示例
- Python hashlib.sha3_256()用法及代碼示例
- Python http.HTTPStatus用法及代碼示例
- 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 http.client.HTTPConnection.set_tunnel用法及代碼示例
- Python hashlib.pbkdf2_hmac用法及代碼示例
- Python hashlib.shake_128()用法及代碼示例
- Python http.client.HTTPConnection用法及代碼示例
注:本文由純淨天空篩選整理自python.org大神的英文原創作品 hex。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。