PHP头条
热点:

Python常用类型转换实现代码实例


本篇文章小编给大家分享一下Python常用类型转换实现代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙伴们可以来看看。

1、byte和str互转

b = b"example"

s = "example"

bytes(s, encoding = "utf8")

str(b, encoding = "utf-8")

2、byte和int互转

b=b'x01x02'

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')

www.phpzy.comtrue/php/39052.htmlTechArticlePython常用类型转换实现代码实例 本篇文章小编给大家分享一下Python常用类型转换实现代码实例,文章代码介绍的很详细,小编觉得挺不错的,现在分享给大家供大家参考,有需要的小伙...

相关文章

    暂无相关文章

PHP之友评论

今天推荐