zoukankan      html  css  js  c++  java
  • 001_bytearray

    bytearray([source [, encoding [, errors]]])

    中文说明:

    bytearray([source [, encoding [, errors]]])返回一个byte数组。Bytearray类型是一个可变的序列,并且序列中的元素的取值范围为 [0 ,255]。

    参数source:

    如果source为整数,则返回一个长度为source的初始化数组;

    如果source为字符串,则按照指定的encoding将字符串转换为字节序列;

    如果source为可迭代类型,则元素必须为[0 ,255]中的整数;

    如果source为与buffer接口一致的对象,则此对象也可以被用于初始化bytearray.。

    版本:在python2.6后新引入,在python3中同样可以使用

    In[2]: a = bytearray(3)
    In[3]: a
    Out[3]: bytearray(b'x00x00x00')
    In[4]: a[0]
    Out[4]: 0
    In[5]: a[1]
    Out[5]: 0
    In[6]: a[2]
    Out[6]: 0
    In[7]: b = bytearray("abc")
    Traceback (most recent call last):
      File "/root/python_dev/.pyenv/versions/3.4.4/lib/python3.4/site-packages/IPython/core/interactiveshell.py", line 2885, in run_code
        exec(code_obj, self.user_global_ns, self.user_ns)
      File "<ipython-input-7-e2c0db524cd1>", line 1, in <module>
        b = bytearray("abc")
    TypeError: string argument without an encoding
    In[8]: b = bytearray(b"abc")
    In[9]: b
    Out[9]: bytearray(b'abc')
    In[10]: b[0]
    Out[10]: 97
    In[11]: b[1]
    Out[11]: 98
    In[12]: b[2]
    Out[12]: 99
    In[13]: c = bytearray([1, 2, 3])
    In[14]: c
    Out[14]: bytearray(b'x01x02x03')
    In[15]: c[0]
    Out[15]: 1
    In[16]: c[1]
    Out[16]: 2
    In[17]: c[2]
    Out[17]: 3
  • 相关阅读:
    datetime和time的时间戳用法
    ER图
    python update()
    理解JWT(JSON Web Token)认证及python实践
    python lambda匿名函数 用法
    flask_restful(转载)
    Flask-SQLALchemy
    创建只有一个元素的元组
    MySQL安装过程中显示无法启动
    聚类
  • 原文地址:https://www.cnblogs.com/arun-python/p/6263729.html
Copyright © 2011-2022 走看看