Get the App
SLTechnology News&Howtos  ›  Development  › 

Example Analysis of character Encoding

Shulou Source: shulou.com Published: 2022-06-02 03:36:41 09月21日 Update

Character coding example analysis, I believe that many inexperienced people do not know what to do, so this paper summarizes the causes of the problem and solutions, through this article I hope you can solve this problem.

# ASCII 2551bytes, GB2312 7000 in 1980, GBK1.0 2w in 1995, GB18030,Unicode 1bytes in 2000, Utfly8 en:1bytes zh:3bytes

# are all backward compatible.

# finally appears Unicode (unified code, universal code, single code)

# ASCII code 1 byte per English station, 8 bits

# Unicode 2 bytes in both Chinese and English, 16 bits

# utf-8 is 1 byte in English and 3 bytes in Chinese

# GBK [decode] converted to unicode encoding and then converted to utf-8 via [encode]

# utf-8 [decode] converted to unicode encoding and then converted to GBK via [encode]

Import sysprint (sys.getdefaultencoding ()) # print the encoding used by default

# encode is all bytes type decode converted to string

# decode is followed by unicode

Q = "you ha" # default unicoude

Print (Q)

Q_gbk=q.encode ("gbk") # defaults to direct conversion from unicoude to gbk

Q_utf8=q.encode () # is converted to utf-8 by default

Print (q_gbk)

Print (q_utf8)

Gbk_to_utf8=q_gbk.decode ("gbk"). Encode ("utf-8") # tell decode that I am gbk to unicode and then encode to utf-8

Print ("utf-8", gbk_to_utf8)

Utf8_to_gbk=q_utf8.decode ("utf-8"). Encode ("gbk") # tell decode that I am utf-8 converted to unicode and then converted to gbk

Print ("gbk", utf8_to_gbk)

S = "you ha"

# convert to gb2312

S_2312=s.encode ("utf-8"). Decode ("utf-8"). Encode ("gb2312")

S_2=s.encode ("gb2312")

Print ("gb2312", sworn 2312)

Print ("gb2", swatches 2)

After reading the above, have you mastered the method of example analysis of character coding? If you want to learn more skills or want to know more about it, you are welcome to follow the industry information channel, thank you for reading!

Tags: Utf-8 coding bytes characters examples analysis content methods more questions English helpless to this end Chinese and English reasons strings this skills articles types Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Linux Docker Apple Microsoft