摘要: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,&
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教程》
相关文章推荐
网站谷歌评分90+意味着什么?2022-09-06
怎样将不安全网站变成安全网站访问?2022-09-26
网站排名下降,可能跟算法更新没关系2022-09-20
网站如何设置高质量的网页标题?2022-09-14
做外贸网站选哪些语言?法语、德语最吃香2022-09-13