python fromhex_Python hex()用法及代码示例

hex()函数是Python3中的内置函数之一,用于将整数转换为相应的十六进制形式。

用法:

hex(x)

参数:

x - an integer number (intobject)

返回: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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值