新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
Python常用類型轉(zhuǎn)換實(shí)現(xiàn)
1.byte和str互轉(zhuǎn)
b = b"example" s = "example" bytes(s, encoding = "utf8") str(b, encoding = "utf-8")
2.byte和int互轉(zhuǎn)
b=b'\\x01\\x02' num=int.from_bytes(b,'little') b1=num.to_bytes(2,'little')
3.byte和float互轉(zhuǎn)
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互轉(zhuǎn)
str1='aaabb' ba=bytearray(str1,encoding='utf-8') str2=ba.decode('utf8')
推薦教程:《Python教程》
當(dāng)前標(biāo)題:Python常用類型轉(zhuǎn)換實(shí)現(xiàn)
鏈接分享:http://www.dlmjj.cn/article/cjpods.html