Python常用类型转换实现代码实例
作者:Mars.wang 时间:2022-05-20 06:03:43
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')
来源:https://www.cnblogs.com/wangbin2188/p/13186325.html
标签:python,类型,转换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
运行asp.net时出现 http错误404-文件或目录未找到
2023-07-24 01:53:36
python利用smtplib实现QQ邮箱发送邮件
2023-07-15 18:47:12
![](https://img.aspxhome.com/file/2023/6/98986_0s.jpg)
浅谈Python响应式类库RxPy
2021-12-24 12:44:26
![](https://img.aspxhome.com/file/2023/3/78453_0s.png)
Python实现计算字符串中出现次数最多的字符示例
2021-03-11 15:59:13
![](https://img.aspxhome.com/file/2023/3/100983_0s.png)
tensorflow训练中出现nan问题的解决
2023-02-10 09:34:09
数据库主键的故事
2008-05-31 07:50:00
ASP日期和时间函数用法详解
2007-10-13 19:33:00
python pyinstaller 加载ui路径方法
2023-06-12 23:27:20
python3实现读取chrome浏览器cookie
2023-10-18 13:18:44
Python 生成一个从0到n个数字的列表4种方法小结
2023-06-10 19:14:59
数学公式“四又二分之一”
2009-01-14 20:03:00
VBScript运行出错信息涵义列表
2007-08-12 18:03:00
Python还能这么玩之用Python做个小游戏的外挂
2022-12-11 18:27:43
![](https://img.aspxhome.com/file/2023/9/72129_0s.png)
基于Python制作一个图片色卡提取器
2023-06-15 14:25:26
![](https://img.aspxhome.com/file/2023/4/78504_0s.png)
pytorch VGG11识别cifar10数据集(训练+预测单张输入图片操作)
2021-10-22 21:55:52
![](https://img.aspxhome.com/file/2023/0/87510_0s.jpg)
了解一下python内建模块collections
2022-03-19 08:32:37
Python基于pandas实现json格式转换成dataframe的方法
2021-08-23 23:09:01
django ModelForm修改显示缩略图 imagefield类型的实例
2021-04-28 16:16:37
基本的页面设计元素布局比例
2007-12-15 09:43:00
python打造爬虫代理池过程解析
2021-10-14 23:43:44