Python function-bytearray
Bytearray ([source [, encoding [, errors])
Chinese description:
Bytearray ([source [, encoding [, errors]) returns a byte array. The Bytearray type is a variable sequence, and the value range of elements in the sequence is [0,255].
Parameter source:
If source is an integer, an initialized array with the source length is returned;
If source is a string, the string is converted to a byte sequence based on the specified encoding;
If source is an iteration type, the element must be an integer in [0,255;
If source is the same as the buffer interface, this object can also be used to initialize bytearray ..
Version: new version after python2.6. It can also be used in python3!
English description:
Return a new array of bytes. the bytearray type is a mutable sequence of integers in the range 0 <= x <256. it has most of the usual methods of mutable sequences, described in Mutable Sequence Types, as well as most methods that the str type has, see String Methods.
The optional source parameter can be used to initialize the array in a few different ways:
If it is a string, you must also give the encoding (and optionally, errors) parameters; bytearray () then converts the string to bytes using str. encode ().
If it is an integer, the array will have that size and will be initialized with null bytes.
If it is an object conforming to the buffer interface, a read-only buffer of the object will be used to initialize the bytes array.
If it is an iterable, it must be an iterable of integers in the range 0 <= x <256, which are used as the initial contents of the array.
Without an argument, an array of size 0 is created.
New in version 2.6.
Example:
>>> a = bytearray(3)>>> abytearray(b'\x00\x00\x00')>>> a[0] >>> a[1] >>> a[2] >>> b = bytearray("abc")>>> bbytearray(b'abc')>>> b[0] >>> b[1] >>> b[2] >>> c = bytearray([1, 2, 3])>>> cbytearray(b'\x01\x02\x03')>>> c[0] >>> c[1] >>> c[2] >>> d = bytearray(buffer("abc"))>>> dbytearray(b'abc')>>> d[0] >>> d[1] >>> d[2]