當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


Python hex()用法及代碼示例


hex()函數是Python3中的內置函數之一,用於將整數轉換為相應的十六進製形式。

用法:

hex(x) 
參數:
x - an integer number (int object)
返回:Returns hexadecimal string.

錯誤和異常:


TypeError:Returns TypeError when anything other than
             integer type constants are passed as parameters.


代碼1:說明了hex()函數的用法。

# Python3 program to illustrate 
# hex() function 
  
print("The hexadecimal form of 23 is"
                            + hex(23)) 
                              
print("The hexadecimal form of the "
      "ascii value is 'a' is " + hex(ord('a'))) 
        
print("The hexadecimal form of 3.9 is "
                        + float.hex(3.9))

輸出:

The hexadecimal form of 23 is0x17
The hexadecimal form of the ascii value os 'a' is 0x61
The hexadecimal form of 3.9 is 0x1.f333333333333p+1


代碼2:將浮點值作為參數傳遞時的Demontsrate TypeError。

# hex() accepts only integer vaues as parameters 
print("The hexadecimal form of 11.1 is "
                            + hex(11.1)) 
  
''' 
# The hexadecimal conversion of floating   
# point integers can be done using the 
# function float.hex() 
print("The hexadecimal form of 11.1 is "  
                    + float.hex(11.1)) 
# Output:
# The hexadecimal form of 11.1 is 0x1.6333333333333p+3 
  
# Similarly, float.hex() throws a TypeError 
# when integer values are passed in it.  
'''

輸出:

Traceback (most recent call last):
  File "/home/7e1ac7e34362fd690cdb72cf294502e1.py", line 2, in 
    print("The hexadecimal form of 11.1 is "+hex(11.1))
TypeError:'float' object cannot be interpreted as an integer


應用範圍:
hex()用於所有標準轉換。例如,十六進製到十進製,十六進製到八進製,十六進製到二進製的轉換。

代碼3:

# TypeConversion from decimal with base 10  
# to hexadecimal form with base 16 
  
  
# Taking input from user  
# an integer with base 10 
number = int(input("Enter a number with base 10\n")) 
  
# The choices present to the user 
print("a. Decimal to Hexadecimal ") 
print("b. Decimal to Octal") 
print("c. Decimal to Binary") 
  
# taking user input 
print("Enter your choice:- ") 
choice = input() 
  
# Running a variable choice 
# Hexadecimal form if choice 
# is set to 'a' 
if choice is 'a':
  
    # lstrip helps remove "0x" from the left  
    # rstrip helps remove "L" from the right, 
    # L represents a long number 
    print("Hexadecimal form of " + str(number) + 
        " is " + hex(number).lstrip("0x").rstrip("L")) 
          
if choice is 'b':
      
    # Octal representation is done 
    # by adding a prefix "0o"  
    print("Octal form of " + str(number) +
        " is " + oct(number).lstrip("0o").rstrip("L")) 
          
if choice is 'c':
      
    # Binary representation is done by 
    # the addition of prefix "0b" 
    print("Binary form of " + str(number) + 
        " is "+bin(number).lstrip("0b").rstrip("L"))

輸出:

Enter a number with base 10
123
a. Decimal to Hexadecimal 
b. Decimal to Octal
c. Decimal to Binary
Enter your choice:- 
a
Hexadecimal form of 123 is 7b
Enter a number with base 10
123456789
a. Decimal to Hexadecimal 
b. Decimal to Octal
c. Decimal to Binary
Enter your choice:- 
a
Hexadecimal form of 123456789 is 75bcd15


相關用法


注:本文由純淨天空篩選整理自retr0大神的英文原創作品 Python | hex() function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。