posts - 9, comments - 1, trackbacks - 0, articles - 0

          2009年12月9日

          character set,character encoding和xml encoding declaration

          hanlray@gmail.com

          Revision: 0.9 Date: 2005/08/28


          1. character set和character encoding

          一 直以為character set和character encoding是一回事,最近才發現它們差不多是兩個概念。從字面意思上看,一個character set就是一些字符的集合,比如26個英文字母,全體漢字,或者兩者的組合,然而計算機是只能處理數字的,一個字符必須被表示成一個數字才可能被計算機處 理,因此一個character set還必須定義字符到數字的映射關系,比如字符A映射到數字64,字符Z映射到數字90,這些數字叫做code points;character encoding則用來定義這些code points如何用字節表示出來,比如code point 90可以被編碼為一個單字節,一個little-endian的unsigned short,一個big-endian的integer,或者更復雜的形式。換句話說,一個character set僅僅是一張字符和數字的映射表,它并不規定這些數字如何用字節表示出來,這種表示的事情由character encoding來定義。因此,一個character encoding是針對一個character set的,而一個character set可以有多個character encoding,比如UTF-8,UTF-16都是Unicode Character Set的encoding。

          2. xml encoding declaration

          對 一段編碼后的文本,要想正確地處理它,必須要事先知道其采用的編碼方式,因此這種編碼信息一般是存在于該文本之外的,比如某些編輯器會在文件開始放幾個不 可見的字節來指示其正文的編碼方式,這些字節叫做BOM(Byte Order Mark);某些網絡協議會有一些字段來指示其所攜帶文本的編碼方式。這種方式很直觀,很多系統/技術采用這種方式,大多數有關xml的應用也會優先使用 這種外部的編碼信息,但是當沒有這種外部的編碼信息可用的時候呢?一個xml document可以用一個xml declaration來聲明其采用的編碼,形如<?xml version="1.0" encoding="UTF-8"?>,這種方式看起來不大可能工作,因為這個聲明本身就是文本的,并且該聲明是xml document的一部分,不可能規定其采用的編碼方式。如何能在不知道xml document編碼的情況下理解其xml declaration中聲明的編碼呢?對xml編碼聲明的位置及內容的限制使自動檢測成為可能:編碼聲明必須出現在文檔開頭,只允許ASCII字符并且 其頭幾個字符必須是<?xml,這樣,一個xml processor就可以先讀出文檔的前幾個字節,推斷其采用的編碼的特征,根據該特征能理解xml declaration,讀出encoding屬性值,從而知道該文檔的編碼。比如,如果前4個字節的內容為3C 3F 78 6D,則可以確定該文檔采用的是一種兼容ASCII的編碼,這樣xml processor就可以用任一種兼容ASCII的編碼(如UTF-8)來理解編碼后xml聲明,因為其只包含ASCII字符,任何兼容ASCII的編碼 對其編碼的結果都是相同的。當得到xml declaration中聲明的編碼時,xml processor再轉換到該編碼對該xml進行處理。下表來自XML W3C Recommendation,列出了自動檢測編碼的方式:

          00 00 00 3C
          3C 00 00 00
          00 00 3C 00
          00 3C 00 00
          UCS-4 or other encoding with a 32-bit code unit and ASCII characters encoded as ASCII values, in respectively big-endian (1234), little-endian (4321) and two unusual byte orders (2143 and 3412). The encoding declaration must be read to determine which of UCS-4 or other supported 32-bit encodings applies.
          00 3C 00 3F UTF-16BE or big-endian ISO-10646-UCS-2 or other encoding with a 16-bit code unit in big-endian order and ASCII characters encoded as ASCII values (the encoding declaration must be read to determine which)
          3C 00 3F 00 UTF-16LE or little-endian ISO-10646-UCS-2 or other encoding with a 16-bit code unit in little-endian order and ASCII characters encoded as ASCII values (the encoding declaration must be read to determine which)
          3C 3F 78 6D UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns for the relevant ASCII characters, the encoding declaration itself may be read reliably
          4C 6F A7 94 EBCDIC (in some flavor; the full encoding declaration must be read to tell which code page is in use)
          Other UTF-8 without an encoding declaration, or else the data stream is mislabeled (lacking a required encoding declaration), corrupt, fragmentary, or enclosed in a wrapper of some kind

          posted @ 2009-12-09 20:27 TonyZhangtl 閱讀(301) | 評論 (0)編輯 收藏

          主站蜘蛛池模板: 仙居县| 剑川县| 本溪市| 德州市| 平果县| 开鲁县| 塔河县| 法库县| 桂林市| 农安县| 竹北市| 乌拉特前旗| 当雄县| 安泽县| 马尔康县| 平安县| 南涧| 洛川县| 六盘水市| 姚安县| 商城县| 遂平县| 额济纳旗| 和田县| 旬阳县| 巨野县| 太仆寺旗| 平罗县| 施甸县| 乌拉特前旗| 都匀市| 修武县| 额尔古纳市| 视频| 汝南县| 灵武市| 个旧市| 孟村| 准格尔旗| 永安市| 松溪县|