SlideShare ist ein Scribd-Unternehmen logo
1 von 20
Downloaden Sie, um offline zu lesen
Unicode NCR 處理
          BrianHsu
   brianhsu.hsu@gmail.com
基本的概念
●   電腦是二進位,只能儲存 0/1 兩種狀態
●   使用對於 bit 的解讀來存放不同類型的資料
–   整數(二補數)
–   浮點數( IEEE754 )
–   字元
ASCII
●   使用一個 BYTE 來儲存字元
●   1byte=8bit=2^8=256 種狀態
●   ASCII 針對前 128 個有統一的定義
–   剩下的 128 個由各系統自行決定應用
     ●   A=65 、 B=66 、 C=67...
     ●   0=48 、 1=49 、 2=50....
–   剩下的 128 個字元,由 CodePage 決定如何解讀
CJK 字元的儲存
●   ASCII 使用的 1 個 byte=256 個字元對英文語
    系很夠用,但漢字圈的字元多達上萬個
–   Unicode 3.1 收錄超過七萬個
●   使用兩個 Byte 來儲存非 ASCII 內定義的字元
萬碼奔騰的年代
●   繁體中文: BIG5
●   簡體中文: GB2312
●   日文: EUC-JP/Shift_JIS...
●   BIG5
–   聯合目錄現行 XML 交換編碼
–   以 1 個 byte 儲存 ASCII 範圍字元
–   以 2 個 byte 儲存中文字元
–   缺字問題
     ●   堃、喆……等
Unicode 編碼
Unicode 解決的問題
●   解決各系統編碼不同的問題
–   只要有字型,可以同時顯示不同國家的語言,如中
     日韓混合
●   已成為網際網路實質標準
●   收錄的漢字比 BIG5 多很多
–   堃、喆
Unicode 的基本概念
●   每個字元對應到一個抽象的 CodePoint
–   CodePoint 是整數數值
–   CodePoint 是抽象的,不是實際上儲存的值
●   為了與 ASCII 相容、 ASCII 內的字元數值和
    Unicode 裡的 CodePoint 相同
–   A=0x0041=65
–   B=0x0042=66
●   其他的字元範例
–   公 =0x516C=20844
–   喆 =0x5586=21894
沒有
「儲存為 Unicode 」
  這種事!!
Unicode 實際儲存時
●   Unicode CointPoint 是抽象的!
●   儲存時需要依照編碼規則儲存
–   UTF-8
–   UTF-16BE
–   UTF-16LE
UTF-8
●   網際網路上的實質資料交換標準
●   使用 1~4 個 byte 來儲存
–   與 ASCII 相容,在 ASCII 範圍內的字使用 1 個 byte
     儲存
–   其於字元視需要使用2~4個 byte 來儲存
UTF-16BigEndean
●   Java 內部字元的儲存方式
–   Java 裡 1 char = 2byte
●   固定使用 2 個 byte 的倍數
–   一個 UnicodePoint 存為 2 個 byte
–   一個 UnicodePoint 存為 4 個 byte
●   若 CodePoint 是在 2^16=65535 內
–   直接儲存 CodePoint 轉二進位後的值
–   A=0x0041
–   堃 =0x5B03
UTF-16BigEndean
●   若字元的 CodePoint 超過 65535
●   使用兩個 Byte 表示
–   並非直接對應 CodePoint ,需經過編碼
–   編碼過後的第一個 Byte 必定為 HighSurrogate 範圍
     內 (0xD800–0xDBFF)
–   言㐌
     ●   CodePoint 0x279A7
     ●   實際編碼
          –   0xD85E 0xDDA7
NCR
●   Numeric character reference
●   使用 Unicode 的 CodePoint 來表示字元
–   用在 XML/HTML 中
–   可以在非 UTF-8/UTF-16 編碼的文件中,表示
     Unicode 的字元
●   型式
–   堃 // 十進位
–   &#0x5803; // 十六進位
–   堃 // 十六進位
NCR
●   可以在 Big5 的文件中表示出 Big5 中沒有的字
●   例:
–   <Title> 游鍚 &#22531;</Title>
–   <Author> 陶 &#21894;</Author>
從 NCR 轉回 UTF-16
●   不要自己做!
●   光是 CodePoint 在 65535 以上字元編碼就很
    麻煩而且容易出錯
●   使用 Apache Commons Language 函式庫
–   JAR檔連結
–   JavaDoc API
從 NCR 轉回 UTF-16
import org.apache.commons.lang3.StringEscapeUtils;

public class Test
{
    public static void main(String [] args) {
        String str1 = " 游鍚 &#22531; 與陶 &#21894;";
        String str2 =
            StringEscapeUtils.unescapeXml(str1);
        String str3 = "&#162215; 懷 ";
        String str4 =
            StringEscapeUtils.unescapeXml(str3);

        System.out.println(str2);
        System.out.println(str4);
    }
}
將 BIG5 中沒有的字轉成 NCR
●   「游鍚堃」轉成「游鍚堃 &#22531; 」
●   將字串轉為字元陣列
–   記得有一個 Unicode 字元對應到四個 Byte 的狀態
–   把字元陣列掃過一次
      ●   先確定是否為 CodePoint 的開頭
      ●   如果是的話
           –   檢查是否為 Big5 中的字元,如果不是就轉成 NCR
      ●   如果不是的話
           –   這是四個 Byte 的狀態的尾巴二個 byte ,不理他
●   參照 tw.digitalarchives.util.TextUtil
將 BIG5 中沒有的字轉成 NCR
import tw.digitalarchives.util.TextUtil;

public class Test
{
    public static void main(String [] args) {
        String str1 = " 游鍚堃與陶喆 ";
        String str2 = " 𧦧懷 ";
        String str3 = TextUtil.normalizeString(str1);
        String str4 = TextUtil.normalizeString(str2);

        System.out.println(str3);
        System.out.println(str4);
    }
}
參考資料
●   Unicode Code Point列表
●   Unicode Code Point查詢

Weitere ähnliche Inhalte

Andere mochten auch

Social media report local governments UK
Social media report local governments UKSocial media report local governments UK
Social media report local governments UKSocialWin
 
Smet Product Applications
Smet Product ApplicationsSmet Product Applications
Smet Product ApplicationsSMET
 
Law Senate Online marketing Strategies
Law Senate Online marketing StrategiesLaw Senate Online marketing Strategies
Law Senate Online marketing StrategiesPooja Sharma
 
8조 피플익스프레스 Play tip
8조 피플익스프레스 Play tip8조 피플익스프레스 Play tip
8조 피플익스프레스 Play tiplucky one plus mis
 
памятники у дніпропетровській області
памятники у дніпропетровській областіпамятники у дніпропетровській області
памятники у дніпропетровській областіknvk35
 
Habilidades comunicativas
Habilidades comunicativasHabilidades comunicativas
Habilidades comunicativasiranialion
 
Dilek boyacioglu publications_10
Dilek boyacioglu publications_10Dilek boyacioglu publications_10
Dilek boyacioglu publications_10guest37328cb
 
Σπουδές στη Φυσική
Σπουδές στη ΦυσικήΣπουδές στη Φυσική
Σπουδές στη ΦυσικήAthanasios Psaltis
 
A survey early detection of
A survey early detection ofA survey early detection of
A survey early detection ofijcsa
 

Andere mochten auch (11)

Social media report local governments UK
Social media report local governments UKSocial media report local governments UK
Social media report local governments UK
 
Preparing for Marketing Automation: Don't Get the Cart Before the Horse
Preparing for Marketing Automation: Don't Get the Cart Before the HorsePreparing for Marketing Automation: Don't Get the Cart Before the Horse
Preparing for Marketing Automation: Don't Get the Cart Before the Horse
 
Smet Product Applications
Smet Product ApplicationsSmet Product Applications
Smet Product Applications
 
Law Senate Online marketing Strategies
Law Senate Online marketing StrategiesLaw Senate Online marketing Strategies
Law Senate Online marketing Strategies
 
8조 피플익스프레스 Play tip
8조 피플익스프레스 Play tip8조 피플익스프레스 Play tip
8조 피플익스프레스 Play tip
 
памятники у дніпропетровській області
памятники у дніпропетровській областіпамятники у дніпропетровській області
памятники у дніпропетровській області
 
Habilidades comunicativas
Habilidades comunicativasHabilidades comunicativas
Habilidades comunicativas
 
Dilek boyacioglu publications_10
Dilek boyacioglu publications_10Dilek boyacioglu publications_10
Dilek boyacioglu publications_10
 
082310 gov team first day 50m
082310 gov team first day 50m082310 gov team first day 50m
082310 gov team first day 50m
 
Σπουδές στη Φυσική
Σπουδές στη ΦυσικήΣπουδές στη Φυσική
Σπουδές στη Φυσική
 
A survey early detection of
A survey early detection ofA survey early detection of
A survey early detection of
 

Ähnlich wie Unicode ncr

编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤jay li
 
Character Encoding - Concepts and Practices
Character Encoding - Concepts and PracticesCharacter Encoding - Concepts and Practices
Character Encoding - Concepts and Practicesrogeryi
 
Python 2 - 快速簡介
Python 2 - 快速簡介Python 2 - 快速簡介
Python 2 - 快速簡介Cheyin L
 
python中文处理
python中文处理python中文处理
python中文处理roamin9 Zhou
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈Xiaozhe Wang
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectHo Kim
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子Justin Lin
 
字符集与编码
字符集与编码字符集与编码
字符集与编码lilizhang
 
MySQL查询优化浅析
MySQL查询优化浅析MySQL查询优化浅析
MySQL查询优化浅析frogd
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3Angel Boy
 

Ähnlich wie Unicode ncr (14)

编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤
 
Character Encoding - Concepts and Practices
Character Encoding - Concepts and PracticesCharacter Encoding - Concepts and Practices
Character Encoding - Concepts and Practices
 
Python 2 - 快速簡介
Python 2 - 快速簡介Python 2 - 快速簡介
Python 2 - 快速簡介
 
python中文处理
python中文处理python中文处理
python中文处理
 
Encoding
EncodingEncoding
Encoding
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈
 
编码大全
编码大全编码大全
编码大全
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding Project
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子
 
字符集与编码
字符集与编码字符集与编码
字符集与编码
 
MySQL查询优化浅析
MySQL查询优化浅析MySQL查询优化浅析
MySQL查询优化浅析
 
Binary exploitation - AIS3
Binary exploitation - AIS3Binary exploitation - AIS3
Binary exploitation - AIS3
 
Nio trick and trap
Nio trick and trapNio trick and trap
Nio trick and trap
 
Python 2-基本語法
Python 2-基本語法Python 2-基本語法
Python 2-基本語法
 

Unicode ncr

  • 1. Unicode NCR 處理 BrianHsu brianhsu.hsu@gmail.com
  • 2. 基本的概念 ● 電腦是二進位,只能儲存 0/1 兩種狀態 ● 使用對於 bit 的解讀來存放不同類型的資料 – 整數(二補數) – 浮點數( IEEE754 ) – 字元
  • 3. ASCII ● 使用一個 BYTE 來儲存字元 ● 1byte=8bit=2^8=256 種狀態 ● ASCII 針對前 128 個有統一的定義 – 剩下的 128 個由各系統自行決定應用 ● A=65 、 B=66 、 C=67... ● 0=48 、 1=49 、 2=50.... – 剩下的 128 個字元,由 CodePage 決定如何解讀
  • 4. CJK 字元的儲存 ● ASCII 使用的 1 個 byte=256 個字元對英文語 系很夠用,但漢字圈的字元多達上萬個 – Unicode 3.1 收錄超過七萬個 ● 使用兩個 Byte 來儲存非 ASCII 內定義的字元
  • 5. 萬碼奔騰的年代 ● 繁體中文: BIG5 ● 簡體中文: GB2312 ● 日文: EUC-JP/Shift_JIS... ● BIG5 – 聯合目錄現行 XML 交換編碼 – 以 1 個 byte 儲存 ASCII 範圍字元 – 以 2 個 byte 儲存中文字元 – 缺字問題 ● 堃、喆……等
  • 7. Unicode 解決的問題 ● 解決各系統編碼不同的問題 – 只要有字型,可以同時顯示不同國家的語言,如中 日韓混合 ● 已成為網際網路實質標準 ● 收錄的漢字比 BIG5 多很多 – 堃、喆
  • 8. Unicode 的基本概念 ● 每個字元對應到一個抽象的 CodePoint – CodePoint 是整數數值 – CodePoint 是抽象的,不是實際上儲存的值 ● 為了與 ASCII 相容、 ASCII 內的字元數值和 Unicode 裡的 CodePoint 相同 – A=0x0041=65 – B=0x0042=66 ● 其他的字元範例 – 公 =0x516C=20844 – 喆 =0x5586=21894
  • 10. Unicode 實際儲存時 ● Unicode CointPoint 是抽象的! ● 儲存時需要依照編碼規則儲存 – UTF-8 – UTF-16BE – UTF-16LE
  • 11. UTF-8 ● 網際網路上的實質資料交換標準 ● 使用 1~4 個 byte 來儲存 – 與 ASCII 相容,在 ASCII 範圍內的字使用 1 個 byte 儲存 – 其於字元視需要使用2~4個 byte 來儲存
  • 12. UTF-16BigEndean ● Java 內部字元的儲存方式 – Java 裡 1 char = 2byte ● 固定使用 2 個 byte 的倍數 – 一個 UnicodePoint 存為 2 個 byte – 一個 UnicodePoint 存為 4 個 byte ● 若 CodePoint 是在 2^16=65535 內 – 直接儲存 CodePoint 轉二進位後的值 – A=0x0041 – 堃 =0x5B03
  • 13. UTF-16BigEndean ● 若字元的 CodePoint 超過 65535 ● 使用兩個 Byte 表示 – 並非直接對應 CodePoint ,需經過編碼 – 編碼過後的第一個 Byte 必定為 HighSurrogate 範圍 內 (0xD800–0xDBFF) – 言㐌 ● CodePoint 0x279A7 ● 實際編碼 – 0xD85E 0xDDA7
  • 14. NCR ● Numeric character reference ● 使用 Unicode 的 CodePoint 來表示字元 – 用在 XML/HTML 中 – 可以在非 UTF-8/UTF-16 編碼的文件中,表示 Unicode 的字元 ● 型式 – &#22531; // 十進位 – &#0x5803; // 十六進位 – &#x5803; // 十六進位
  • 15. NCR ● 可以在 Big5 的文件中表示出 Big5 中沒有的字 ● 例: – <Title> 游鍚 &#22531;</Title> – <Author> 陶 &#21894;</Author>
  • 16. 從 NCR 轉回 UTF-16 ● 不要自己做! ● 光是 CodePoint 在 65535 以上字元編碼就很 麻煩而且容易出錯 ● 使用 Apache Commons Language 函式庫 – JAR檔連結 – JavaDoc API
  • 17. 從 NCR 轉回 UTF-16 import org.apache.commons.lang3.StringEscapeUtils; public class Test { public static void main(String [] args) { String str1 = " 游鍚 &#22531; 與陶 &#21894;"; String str2 = StringEscapeUtils.unescapeXml(str1); String str3 = "&#162215; 懷 "; String str4 = StringEscapeUtils.unescapeXml(str3); System.out.println(str2); System.out.println(str4); } }
  • 18. 將 BIG5 中沒有的字轉成 NCR ● 「游鍚堃」轉成「游鍚堃 &#22531; 」 ● 將字串轉為字元陣列 – 記得有一個 Unicode 字元對應到四個 Byte 的狀態 – 把字元陣列掃過一次 ● 先確定是否為 CodePoint 的開頭 ● 如果是的話 – 檢查是否為 Big5 中的字元,如果不是就轉成 NCR ● 如果不是的話 – 這是四個 Byte 的狀態的尾巴二個 byte ,不理他 ● 參照 tw.digitalarchives.util.TextUtil
  • 19. 將 BIG5 中沒有的字轉成 NCR import tw.digitalarchives.util.TextUtil; public class Test { public static void main(String [] args) { String str1 = " 游鍚堃與陶喆 "; String str2 = " 𧦧懷 "; String str3 = TextUtil.normalizeString(str1); String str4 = TextUtil.normalizeString(str2); System.out.println(str3); System.out.println(str4); } }
  • 20. 參考資料 ● Unicode Code Point列表 ● Unicode Code Point查詢