python進制,python 核心數_Python hex()

 2023-11-19 阅读 25 评论 0

摘要: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 t

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將負責將它們轉換為十六進制格式。

Python hex()示例 (Python hex() example)

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

帶有對象的Python hex() (Python hex() with object)

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
GitHub Repository.GitHub存儲庫中檢出完整的python腳本和更多Python示例。

Reference: Official Documentation

參考: 官方文檔

翻譯自: https://www.journaldev.com/22902/python-hex

python 核心數

版权声明:本站所有资料均为网友推荐收集整理而来,仅供学习和研究交流使用。

原文链接:https://hbdhgg.com/1/183212.html

发表评论:

本站为非赢利网站,部分文章来源或改编自互联网及其他公众平台,主要目的在于分享信息,版权归原作者所有,内容仅供读者参考,如有侵权请联系我们删除!

Copyright © 2022 匯編語言學習筆記 Inc. 保留所有权利。

底部版权信息