關於ASCII、Unicode、UTF-8編碼問題的小思考

1、前言

僅僅只是關於編碼問題的一些小思考,簡單過一下。bash

2、關於編碼

字符編碼問題.jpg

3、驗證

以往咱們可能瞭解的都是一些理論知道,下面咱們來經過 Python3 來驗證一下。分別來看看英文字符 'A' 和 '中' 分別在不一樣編碼下的實際狀況。編碼

A 的 ASCII 、UTF-八、GB2312 編碼spa

>>> 'A'.encode('ascii')
b'A'
複製代碼
>>> 'A'.encode('utf-8')
b'A'
複製代碼
>>> 'A'.encode('gb2312')
b'A'
複製代碼

中的 ASCII 、UTF-八、GB2312 編碼code

>>> '中'.encode('ascii')
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode character '\u4e2d' in position 0: ordinal not in range(128) 複製代碼
>>> '中'.encode('utf-8')
b'\xe4\xb8\xad'
複製代碼
>>> '中'.encode('gb2312')
b'\xd6\xd0'
複製代碼

能夠看到中文是不能進行 ASCII 編碼的。cdn

相關文章
相關標籤/搜索