Python built-in function-bytearray

Source: Internet
Author: User

English documents:

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

Return a new array of bytes. The bytearray class is a mutable sequence of integers in the range 0 <= x < 256. It has the most of the usual methods of mutable sequences, described in mutable Sequence Types, as well as most methods that T He bytes type has, see Bytes and Bytearray Operations.

The optional source parameter can be used to initialize the array in a few different ways:

    • If it was a  string , you must also give the  encoding   (and Optiona lly,  errors ) parameters; bytearray ()  then converts the string to bytes Using str.encode () .
    • If it is an  integer , the array would have a that size and would be initialized with null bytes.
    • If It is an object conforming to the  buffer  interface, a read-only buffer of the object would be D to initialize the bytes array.
    • If it is an  iterable , it must be a iterable of integers in the Range 0 <= x < < Span class= "Pre" >256 , which is used as the initial contents of the array
    • /ul>
    def __init__(Self, Source=none, Encoding=none, errors='Strict'):#known special case of bytearray.__init__        """ByteArray (iterable_of_ints), ByteArray ByteArray (String, encoding[, errors]), ByteArray ByteArray (bytes_or_buffer), mutable copy of Bytes_or_buffer bytearray (int), bytes array of size given B Y the parameter initialized with null bytes ByteArray (), empty bytes array Construct a mutable ByteArray object from:-an iterable yielding integers in range ()-a text string encoded using the S          Pecified encoding-a bytes or a buffer Object-any object implementing the buffer API. -An integer # (copied from class Doc)"""        Pass
ByteArray

Returns a new byte array.
The ByteArray class is a variable sequence of range 0 < = x < 256.
It has common methods for most mutable sequences, described in variable sequence types, and methods for most byte types, see Byte and ByteArray operations.

The optional source parameter can initialize the array in several different ways:

    • If it is a string, then you must also give the encoding (and optionally the error) argument, ByteArray () and then use Str.encode () to convert the string to bytes.
    • If it is an integer, the array will have this size and will be initialized with a null byte.
    • If it is an object that conforms to the buffer interface, the byte array is initialized with the object's read-only buffer.
    • If it is iterative, then it must be an iteration of an integer of range 0 < = x < 256, which is used as the initial content of the array

If there are no parameters, an array of size 0 is created.

Description
1. The return value is a new byte array
2. Returns a byte array with a length of 0 when none of the 3 parameters are passed

#!/usr/bin/python3b = ByteArray () print (b)
Print (len (b))

Results:

ByteArray (b ') 0

3. The encoding parameter must also be supplied when the source parameter is a string, and the function converts the string to a byte array using the Str.encode method

b = ByteArray (' Chinese ')

Results:

Traceback (most recent):  File "d:/py/day001/day1/test.py", line 3, in <module>    B = ByteArray (' Chinese ') ) typeerror:string argument without an encoding

The encoding parameter must also be supplied, and the function converts the string using the Str.encode method to a byte array

b = ByteArray (' Chinese ', ' utf-8 ') print (b) print (Len (b))

Results:

ByteArray (b ' \xe4\xb8\xad\xe6\x96\x87 ') 6

4. Returns an empty byte array of the length specified by this integer when the source parameter is an integer

#!/usr/bin/python3b = ByteArray (5) print (b) print (Len (b))

Execution Result:

ByteArray (b ' \x00\x00\x00\x00\x00 ') 5

5. When the source parameter is a type object that implements the buffer interface, a read-only method is used to read the bytes to the byte array and return

#!/usr/bin/python3b = ByteArray ([1,2,3,4,5]) print (b) print (Len (b))

Execution Result:

ByteArray (b ' \x01\x02\x03\x04\x05 ') 5

6. When the source parameter is an iterative object, the elements of the iteration object must conform to 0 <= x < 256 so that it can be initialized into the array

#!/usr/bin/python3b = ByteArray ([1,2,3,4,5,256]) print (b) print (Len (b))

Execution Result:

Traceback (most recent):  File "d:/py/day001/day1/test.py", line 3, in <module>    b = ByteArray ([ , 3,4,5,256]) Valueerror:byte must is in range (0, 256)

  

Python built-in function-bytearray

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.