zoukankan      html  css  js  c++  java
  • 汉字编码

    汉字编码


    一、汉字所占的字节数

    对于一个字符串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、理解几个概念

    ①. termencoding
    
    Encoding 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. 
    终端使用的编码方式,指定了键盘输出的编码格式和屏幕能够正常显示的编码方式
    
    ②. encoding
    
    Sets 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,以免影响正常工作。
    
    ③. fileencoding
    
    Sets 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,我们可以将文件由一种编码转换为另一种编码
    
    ④. fileencodings
    
    This 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 的时候,一定要把要求严格的、当文件不是这个编码的时候更容易出现解码失败的编码方式放在前面,把宽松的编码方式放在后面。
    
    

    假如encodingfileencoding不匹配,比如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

  • 相关阅读:
    3月16日
    11月8日
    Code4 APP
    为什么alertView弹出后button会消失的问题
    设置某个类使用或者禁用ARC
    Bundle使用&NSBundle
    respondsToSelector
    NSDate获取当前时区的时间
    iOS enum 定义与使用
    IOS开发之纯代码界面--基本控件使用篇 ┊
  • 原文地址:https://www.cnblogs.com/xuanyuanchen/p/5844567.html
Copyright © 2011-2022 走看看