First, Python3 the text and binary data to make a distinction. The text is Unicode encoded, str type, for display. The binary type is the bytes type, which is used for storage and transport. Bytes is a sequence of byte, and STR is a sequence of Unicode.
STR type:
1 >>> s = u ' Hello '2 >>> s3' Hello ' 4 >>> type (s)5 <class'str' >
Bytes Type:
1 >>> b = b'abc'2 >>> b3 b' ABC ' 4 >>> type (b)5 <class'bytes' >
Ii. conversion Relationship between STR and bytes: Str-->encode ()-->bytes-->decode ()-->str
Conversion Mode one: Encode (), decode ()
1>>> a = u'Hello'2>>> B = A.encode ('Utf-8')3>>>b4B'\XE4\XBD\XA0\XE5\XA5\XBD'5>>>type (b)6<class 'bytes'>7>>> new_a = B.decode ('Utf-8')8>>>new_a9 'Hello'Ten>>>type (new_a) One<class 'Str'>
Conversion Mode II: bytes (), str ()
1>>> a = u'Hello'2>>> b= bytes (A, encoding='Utf-8')3>>>b4B'\XE4\XBD\XA0\XE5\XA5\XBD'5>>>type (b)6<class 'bytes'>7>>> new_a = str (b, encoding='Utf-8')8>>>new_a9 'Hello'Ten>>>type (new_a) One<class 'Str'>
Iii. Types of ByteArray
The ByteArray class is a variable sequence of range 0 < = x < 256.
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.
When the source parameter is a string:
1 >>> b = bytearray (u' hello ', encoding='utf-8' ) )2 >>> b3 bytearray (b'\xe4\xbd\xa0\xe5\xa5\xbd ')4 >>> type (b)5 <class' bytearray'>
When the source parameter is an integer:
1 >>> b = ByteArray (5)2 >>> b3 bytearray (b' \x00\x00\x00\x00\x00 ' )4 >>> type (b)5 <class'ByteArray '>
When the source parameter is an iterative object, the elements of the iteration object must conform to 0 <= x <:
1 >>> b = ByteArray ([1, 2, 3, 4, 255])2 >>> b3 ByteArray (b ' \x01\x02\x03\x04\xff ' )4 >>> type (b)5 <class'ByteArray '
Iv. differences between bytes and ByteArray
Bytes is immutable, with Str. The ByteArray is mutable, with the list.
1>>> B =ByteArray ()2>>>b3ByteArray (b"')4>>> B.append (10)5>>>b6ByteArray (b'\ n')7>>> B.append (100)8>>>b9ByteArray (b'\nd')Ten>>> B.remove (100) One>>>b AByteArray (b'\ n') ->>> b.insert (0, 150) ->>>b theByteArray (b'\x96\n') ->>> B.extend ([1, 3, 5]) ->>>b -ByteArray (b'\x96\n\x01\x03\x05') +>>> B.pop (2) -1 +>>>b AByteArray (b'\x96\n\x03\x05') at>>>B.reverse () ->>>b -ByteArray (b'\x05\x03\n\x96') ->>>b.clear () ->>>b -ByteArray (b"')
V. Bytes and ByteArray conversions
1 >>> b = b'abcdef'2 >>> bay = ByteArray (b) 3 >>> Bay4 bytearray (b'abcdef') 5 >>> b = bytes (bay)6 >>> b7 b' abcdef '
Vi. ByteArray and Str conversions
1>>> A ='abcdef'2>>> B = ByteArray (A, encoding='Utf-8')3>>>b4ByteArray (b'abcdef')5>>> a = B.decode (encoding='Utf-8')6>>>a7 'abcdef'
Python string type bytes type ByteArray type