1 sequence
Common sequences are: List tuple str bytes (byte string) ByteArray
2-byte array ByteArray
A variable sequence of bytes, equivalent to a variable version of bytes.
3 Creating a function ByteArray
To create a method rule for a ByteArray object
ByteArray () ByteArray (integer-N) bytearray (integer-iterated object)
ByteArray (b ' string ')
ByteArray (string, encoding='utf-8')
Example:
>>>ByteArray () ByteArray (b"')>>> ByteArray ([a]) ByteArray (b'\x01\x02\x03')>>> ByteArray (["a","b","C"]) Traceback (most recent): File"<stdin>", Line 1,inch<module>Typeerror:an integer isRequired>>> ByteArray (3) ByteArray (b'\x00\x00\x00')>>> ByteArray ("ABC", encoding="Utf-8") ByteArray (b'ABC')>>> ByteArray ("ABC") Traceback (most recent): File"<stdin>", Line 1,inch<module>typeerror:string argument without an encoding
4 arithmetic operations
byte arrays operate in a similar way to lists
+ + = * *= comparison operation :< <= > >= = = =in/not in/slice Slice
Example
>>> a = ByteArray (b"ABCDE")>>>A[0]97>>>Print(a) ByteArray (b'ABCDE')>>> a[1:] = ByteArray (b'BCDE')#Slice Assignment>>>Abytearray (b'ABCDE')>>> A[0] = 0x41#single-element assignment, the ASCII value of a 0x41>>>Abytearray (b'ABCDE')>>> B = Bytes (a)#to convert a byte array to a byte string>>>BB'ABCDE'>>> C = ByteArray (b)#to turn a byte string into a byte array>>>CByteArray (b'ABCDE')
5 ByteArray Common methods
Method |
Description |
BA represents a byte array |
|
Ba.clear () |
Empty |
Ba.append (N) |
Append one byte (n is an integer of 0~255) |
Ba.remove (value) |
Deletes the first occurrence of the byte, or generates a valueerror error if none |
Ba.reverse () |
BYTE order reversal |
Ba.decode (encoding= ' utf-8 ') |
Convert to String |
Ba.find (sub[, Start[,end]]) |
Find sub-byte string |
6 The difference between bytes, ByteArray and str
Byte string bytes, byte array bytearray is a sequence of binary data, each of which consists of 8bit binary (with 1byte, the same 2-bit 16 binary number, and 0~255).
The number of bytes in the computer's language, the strings are human languages, and they form a one by one correspondence between the coded tables.
The smallest ASCII
encoding table only needs one byte, and only occupies the [31,127]
code bit;
STR storage Unicodel 6 characters (0~65535)
Bytes/bytearry = Str.encode (encoding="utf-8"= Bytes/bytearray.decode (encoding ="utf-8")
Encode: Encoding, decode: decoding
Python Learning note 015--sequence (byte array bytearray)