码迷,mamicode.com
首页 > 编程语言 > 详细

Python函数-bytearray()

时间:2017-05-24 00:48:19      阅读:418      评论:0      收藏:0      [点我收藏+]

标签:errors   buffer   长度   str   字节序   source   可变   color   erro   

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

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

 

参数source:

 

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

 

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

 

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

 

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

 

实例:

 1 >>> a = bytearray(3)
 2 >>> a
 3 bytearray(b\x00\x00\x00)
 4 >>> a[0]
 5   
 6 >>> a[1]
 7   
 8 >>> a[2]
 9   
10 >>> b = bytearray("abc")
11 >>> b
12 bytearray(babc)
13 >>> b[0]
14    
15 >>> b[1]
16   
17 >>> b[2]
18   
19 >>> c = bytearray([1, 2, 3])
20 >>> c
21 bytearray(b\x01\x02\x03)
22 >>> c[0]
23   
24 >>> c[1]
25   
26 >>> c[2]
27   
28 >>> d = bytearray(buffer("abc"))
29 >>> d
30 bytearray(babc)
31 >>> d[0]
32   
33 >>> d[1]
34   
35 >>> d[2]

 

Python函数-bytearray()

标签:errors   buffer   长度   str   字节序   source   可变   color   erro   

原文地址:http://www.cnblogs.com/guyuyuan/p/6896714.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!