python bytes、int、str、float互轉


1.bytes轉化為int

  函數格式:int.from_bytes(bytes, byteorder, *, signed=False)

  

s1 = b'\xf1\xff'
print(int.from_bytes(s1, byteorder='big', signed=False))
print(int.from_bytes(s1, byteorder='little', signed=True))

  運行結果:

F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo2.py
61951
-15

Process finished with exit code 0

2.bytes轉化為str

  str(bytes,[encoding,error])

  decode(encoding)(python3)

  

b=b"hell0 world"
print('bytes --> str')
print(str(b, encoding="utf-8"))
print(str(b))    #默認utf8編碼

  運行結果:

F:\dev\python\python.exe F:/pyCharm/L02_Test/L02Interface/L02_Common/try_demo2.py
bytes --> str
hell0 world
b'hell0 world'

Process finished with exit code 0

 3. int轉為bytes

n=46000
print(n.to_bytes(length=2,byteorder='big',signed=False))

  

 

 

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM