a= ' I'm okay ' # # # #python3 default encoding for Unicode
# # #unicode >gb2312
unicode_gb2312=a.encode (' gb2312 ') # # #因为默认是unicode所以不需要decode (), direct encode Chengxiang to convert the encoding such as gb2312
print (' My gb2312 ', unicode_gb2312) # # #返回结果: my gb2312 B ' \xce\xd2\xba\xdc\xba\xc3 '
# # #gb2312 >utf8
Gb2312_utf8=unicode_gb2312.decode (' gb2312 '). Encode (' Utf-8 ') # #当前字符为gb2312所以要先decode成unicode ( The parameter passed in decode is the encoding set of the current character and then encode into Utf-8
Print (' I'm Utf-8 ', Gb2312_utf8) # # #返回结果: I'm utf-8 B ' \xe6\x88\x91\xe5\xbe\x88\xe5\xa5\xbd '
# # #utf8 >GBK
utf8_gbk=gb2312_utf8.decode (' Utf-8 '). Encode (' GBK ')# # The current character set is encoded as Utf-8 to convert to GBK first decode into a Unicode character set and then encode to GBK character set
Print ("I am GBK", UTF8_GBK) # # #返回结果: I am gbk b ' \xce\xd2\xba\xdc\xba\xc3 '
# # #utf8 >uicode
utf8_unicode=utf8_gbk.decode (' GBK ') # # # #注意当转换成unicode时 do not need encode ()
Print (' I'm Unicode ', Utf8_unicode) # # #返回结果: I'm Unicode I'm fine.
# # #unicode >gb18030
unicode_gb18030=utf8_unicode.encode (' gb18030 ')
Print (' I'm gb18030 ', unicode_gb18030) # # #返回结果: I'm gb18030 b ' \xce\xd2\xba\xdc\xba\xc3 '
# # #总结各个编码的互相转换都要先转换成unicode然后通过unicode再转换成想要的编码
# #从上面可以看出gb2312, the results of gbk,gb18030 return are the same, it should be for these 3 are Chinese code, so are all downward compatible with each other
# #中国的编码最先出来的是gb2312, then GB18030, and finally GBK, the number of characters they support is also increasing in order from the first 7,000 to the present nearly 30,000
Conversion of individual character encodings in Python3