Discuss / Python / 调用函数,作业

调用函数,作业

Topic source

谁能给说说,关于float和str的hex结果,没有16进制吗?

# -*- coding: utf-8 -*-
n1=255
n2=1000
n3=12.34
n4='a'
print(n1,'的十六进制是:',hex(n1))
print(n2,'的十六进制是:',hex(n2))

print('%d的十六进制是:%s \n%d的十六进制是:%s'%(n1,hex(n1),n2,hex(n2)))
print('0x%x,0x%x'%(n1,n2))

print(n3,'的十六进制是:',hex(n3))
#    TypeError: 'float' object cannot be interpreted as an integer
print(n4,'的十六进制是:',hex(n4))
#    TypeError: 'str' object cannot be interpreted as an integer

ElegantFlower

#2 Created at ... [Delete] [Delete and Lock User]

有的

浮点数转十六进制

print(float.hex(9.8))

字符串转十六进制

int('0x1e',16) binascii.b2a_hex(u"你好啊".encode("utf8"))

关于字符串转十六进制,能再帮我看看到底哪里不对吗?

n4='你好'
print(n4,'的十六进制是:',binascii.b2a_hex(u"你好".encode("utf8"))

另外,int('0x1e',16)是什么用处,不理解啊

不好意思,我自己找到原因了! 非常感谢~

n4='你好'
import binascii
print(n4,'的十六进制是:',binascii.b2a_hex(n4.encode("utf8")))

  • 1

Reply