zoukankan      html  css  js  c++  java
  • python bytes、int、str、float互转

    1.bytes转化为int

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

      

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

      运行结果:

    F:devpythonpython.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:devpythonpython.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))

      

      

  • 相关阅读:
    (一)maven基本配置,概念,常用命令
    redis 小结
    git 小结
    spring.xml
    servlet web.xml学习笔记
    springmvc小试牛刀
    maven
    springmvc学习笔记1
    springmvcpojo
    springmvc学习笔记
  • 原文地址:https://www.cnblogs.com/linwenbin/p/12035165.html
Copyright © 2011-2022 走看看