Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Example Analysis of character Encoding

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/02 Report--

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!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report