![图片[1]-Python中int与bytes相互转换-uusu优素-乐高,模型,3d打印,编程](http://uusucn.zbbe.cn/wp-content/uploads/2024/01/1607921375736189.png)
我们在使用Python的过程中,会遇到这种情况:需要将接收的bytes数据转换为整形数,或者是将整形数转换成bytes数据。之前小编介绍过在Python中可以转换为整形数的int函数。本文小编就介绍Python中int与bytes如何相互转换的过程:int.to_bytes()和int.from_bytes()。
1、int.to_bytes()
defintToBytes(value,length): result=[] foriinrange(0,length): result.append(value>>(i*8)&0xff) result.reverse() returnresult
2、int.from_bytes()
1#bytes与int
2b=b'\x01\x02'
3num=int.from_bytes(b,'little')
4print('bytes转int:',num)
5
输出
513原文来自:https://www.py.cn
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END
    
















































暂无评论内容