A. The bytes used for each encoding.
Ascii
a:00000010 8 bits of one byte
Unicode a:00000000 00000001 00000010 00000100 32 bits four bytes
Medium: 00000000 00000001 00000010 00000110 32 bit four bytes
Utf-8 a:00100000 8 Bit one byte
Medium: 00000001 00000010 00000110 24 bits Three bytes
GBK a:00000110 8 Bit one byte
Medium: 00000010 00000110 16 bit two bytes
1, the binary system between each encoding, it can not recognize each other, will produce garbled.
2, file storage, transmission, cannot be Unicode (only utf-8 utf-16 gbk,gb2312,asciid, etc.), because Unicode occupies too much space.
Second, the encoding method in Python3:
STR is encoded in memory in Unicode and cannot be stored or transmitted directly.
Bytes Type: Basically the same as STR, but the encoding is different.
For English:
STR: representation: s = ' Alex ' (WYSIWYG)
Encoding method: 010101010 Unicode encoding
Bytes: representation: s = B ' Alex ' (WYSIWYG)
Encoding method: 000101010 Utf-8, GBK and other encoding methods.
For Chinese:
STR: representation: s = ' China '
Encoding method: 010101010 Unicode
Bytes: expression form: s = B ' x\e91\e91\e01\e21\e31\e32 '
Encoding method: 000101010 utf-8 GBK ....
Third, the code. Encode convert Unicode to utf-8 or GBK (expression is str converted to bytes)
S1 = "Alex"
S11 = S1.encode ("Utf-8")
S12 = S1.encode ("GBK")
S2 = "China"
S22 = S2.encode ("Utf-8")
S23 = S2.encode ((' GBK '))
Print (S11,S12,S22,S23)
Python Coding issues