漢字編碼
一、漢字所占的字節數
對于一個字符串sizeof("請放手")
,結果值是4。測試操作系統:Centos 6.4,硬件平臺:Windows 7 32位 + VirtualBox 4.3.12。看來用sizeof()
來計算漢字所占用的字節或空間是不準確的。strlen("請放手")
,返回值是9。
二、漢字編碼問題
在使用libxml2
輸出的時候,出現了占用空間不一致的問題。
讀出的漢字用的是3位
的UTF-8
編碼,在另外一個系統中用的是2位
的GB2312
編碼。
比如:漢
的GB2312
Unicode
UTF-8
值分別對應于
GB2312 | Unicode | UTF-8 |
---|---|---|
BABA | 6C49 | E6B189 |
見:GB2312 to UTF-8 - Row 26: 0xBAA1 to 0xBAFE '漢'
在對提取的漢字進行顯示的時候,尤其是系統對編碼的支持有限定要求的時候,需要確定字符的編碼格式,否則出來的全部都是亂碼。
三、GB2312與UTF-8的轉換
REFER:漢字編碼對照表(gb2312/unicode/utf8) 【轉帖】
REFER:吳劍: UTF-8, Unicode, GB2312格式串轉換之C語言版
REFER:GNU libiconv
四、VIM漢字編碼與顯示問題)
Note:在VIM中輸入漢字的小技巧,當文件的fileencoding/編碼格式是UTF-8的時候,如果沒有漢語拼音輸入法,輸入漢字可以用一種新的方法,那就是CTRL+q+u+xxxx
,其中xxxx是漢字的Unicode編碼,不是UTF-8編碼。
1、常用的漢字編碼
①GB2312
GB2312字符集中除常用簡體漢字字符外還包括希臘字母、日文平假名及片假名字母、俄語西里爾字母等字符,未收錄繁體中文漢字和一些生僻字。可以用繁體漢字測試某些系統是不是只支持GB2312編碼。
②GBK
GBK編碼是 GB2312 編碼的超集,向下完全兼容GB2312,同時GBK收錄了Unicode基本多文種平面中的所有C-J-K漢字。同 GB2312一樣,GBK也支持希臘字母、日文假名字母、俄語字母等字符,但不支持韓語中的表音字符(非漢字字符)。GBK還收錄了GB2312不包含的 漢字部首符號、豎排標點符號等字符。
③CP936
④EUC_CN
Extended Unix Code (EUC) is a multibyte character encoding system used primarily for Japanese, Korean, and simplified Chinese.
⑤GB18030
GB18030編碼向下兼容GBK和GB2312,兼容的含義是不僅字符兼容,而且相同字符的編碼也相同。GB18030收錄了所有Unicode3.1中的字符,包括中國少數民族字符,GBK不支持的韓文字符等等,也可以說是世界大多民族的文字符號都被收錄在內。
GBK和GB2312都是雙字節等寬編碼,如果算上和ASCII兼容所支持的單字節,也可以理解為是單字節和雙字節混合的變長編碼。
2、理解幾個概念
①. termencodingEncoding used for **the terminal**. This specifies what character encoding the keyboard produces and the display will understand. For the GUI it only applies to the keyboard.
終端使用的編碼方式,指定了鍵盤輸出的編碼格式和屏幕能夠正常顯示的編碼方式②. encodingSets the character encoding used inside Vim. It applies to text in the buffers, registers, Strings in expressions, text stored in the vim info file, etc. Changing this option will not change the encoding of the existing text in vim. The character encoding of files can be different from **encoding**. This is specified with **file encoding**. The conversion is done with iconv() or as specified with ‘charconvert'
encoding適用于vim的內部緩存、寄存器、字符串、vim info 文本等的編碼方式。改變encoding,不會改變vim中當前文件的編碼格式。文件的編碼由fileencoding來指定,與 encoding 不一樣。可以通過iconv()或'charconver'來轉換。
如果工作用的編碼中含有無法轉換為內部編碼的字符,在這些字符就會丟失。因此,在選擇 Vim 的內部編碼的時候,一定要使用一種表現能力足夠強的編碼,例如UTF8,以免影響正常工作。③. fileencodingSets the character encoding for the file of this buffer. When 'file encoding' is different from 'encoding', conversion will be done when reading and writing the file.
文件自身的編碼方式。如果fileencoding和encoding不一致,那么在讀取和寫入文件時,會對編碼方式進行轉換。
通過打開文件后設置 fileencoding,我們可以將文件由一種編碼轉換為另一種編碼④. fileencodingsThis is a list of character encodings considered when starting to edit an existing file. When a file is read, Vim tries to use the first mentioned character encoding. If an error is detected, the next one in the list is tried. When an encoding is found that works, 'file encoding' is set to it. If all fail, 'file encoding' is set to an empty string, which means the value of 'encoding' is used.
Note that 'fileencodings' is not used for a new file, the global value of 'file encoding' is used instead.
是一個字符編碼的列表。當vim打開一個文件時,會嘗試使用列表中的第一個字符編碼方式。如果檢測到錯誤,就用列表中的下一個。當找到一個可以正常工作的字符編碼方式后,file encoding就被設置成找到的字符編碼方式。如果最后都失敗了,fileencoding就被設置成空的,這意味者字符的編碼方式就跟 encoding變量的值一樣了。這樣通過這個列表,Vim可以自動判斷文件的編碼,自動判斷失敗時還可手動設定 fileencoding 來指定編碼。因此,設置 fileencodings 的時候,一定要把要求嚴格的、當文件不是這個編碼的時候更容易出現解碼失敗的編碼方式放在前面,把寬松的編碼方式放在后面。
假如encoding
和fileencoding
不匹配,比如encoding=utf-8,但是fileencoding=big5,則在保存文件的時候會提示:E513 write error conver
.
REFER:shell和vim中亂碼原因及消除辦法
REFER:Vim 編碼/Coding
五、讓Linux Bash顯示多種漢字編碼方案
1、顯示當前的編碼方案env | grep LANG
2、list系統支持的、所有的編碼方案
參考:
1、Python libxml2 parsing xml having Chinese characters
2、GB2312與UTF-8對照表
3、shell和vim中亂碼原因及消除辦法
4、Software Developer Must Know About Unicode and Character Sets
Please do not write another line of code until you finish reading this article.
5、CJKV Information Processing: CJKV Computing
6、
7、Vim and unicode keybindings: math, IPA, and more