Python常用类型转换实现

1.byte和str互转

创新互联公司专注于青铜峡网站建设服务及定制,我们拥有丰富的企业做网站经验。 热诚为您提供青铜峡营销型网站建设,青铜峡网站制作、青铜峡网页设计、青铜峡网站官网定制、小程序制作服务,打造青铜峡网络公司原创品牌,更为您提供青铜峡网站排名全网营销落地服务。
b = b"example" 
s = "example" 
bytes(s, encoding = "utf8") 
str(b, encoding = "utf-8")

2.byte和int互转

b=b'\\x01\\x02'
num=int.from_bytes(b,'little')
b1=num.to_bytes(2,'little')

3.byte和float互转

import struct
s=b'@zQ\\x16'
def byteToFloat(b):
  return struct.unpack('!f',s)[0]

def floatToBytes(f):
  bs = struct.pack("f",f)
  return bytes((bs[3],bs[2],bs[1],bs[0]))
f1=byteToFloat(s)
floatToBytes(f1)

4.str和bytearray互转

str1='aaabb'
ba=bytearray(str1,encoding='utf-8')
str2=ba.decode('utf8')

推荐教程:《Python教程》
新闻名称:Python常用类型转换实现
标题来源:http://lszwz.com/article/cjpods.html

其他资讯

售后响应及时

7×24小时客服热线

数据备份

更安全、更高效、更稳定

价格公道精准

项目经理精准报价不弄虚作假

合作无风险

重合同讲信誉,无效全额退款