SlideShare a Scribd company logo
1 of 15
Download to read offline
Character Encoding
           Concepts and Practices
           Roger
           yixx@ucweb.com
           roger2yi@gmail.com
           www.twitter.com/roger2yi

2011/6/1                              1
Concepts




2011/6/1              2
Definition
• A character encoding system      • 简而言之,字符编码就是
  consists of a code that pairs      将字符通过编码转换成另
  each character from a given
  repertoire with something
                                     外一种表现形式,可能是
  else, such as a sequence of        自然数的序列,八位组,
  natural numbers, octets or         电子脉冲等
  electrical pulses, in order to    – 从上面的定义看,摩斯码,
  facilitate the transmission of      电报码也是字符编码的一种
  data (generally numbers             方式
  and/or text) through              – 当然我们一般说的字符编码
  telecommunication networks          实际上是以八位组的方式编
  or storage of text in               码,使用计算机进行显示,
  computers.                          存储和传输


2011/6/1                                           3
Common Character Encodings
• 常见的计算机字符编码可以分为3大类

• ASCII及其扩展编码
    – ASCII
    – ISO 8859 1~10 13 ~16(aka Latin编码)
    – ISO 8859 11 Thai




2011/6/1                                  4
Cont.




2011/6/1           5
Cont.
• CJK(中日韩)东亚语言的编码
    – 大陆简体中文的国标编码 — GB2312,GBK,GB18030
    – 台湾繁体中文 Big5(大5码)
    – 日语 Shift JIS, EUC-JP,ISO-2022-JP
    – 朝鲜语 EUC-KR,ISO-2022-KR




2011/6/1                                 6
Cont.
• Unicode编码
    – UTF-8
    – UTF-16 (BE/LE)
           • UCS-2 是UTF-16的一个子集,我们在说UTF-16的时候实际包括了
             UCS-2,后面会讲到两者的区别
    – UTF-32 (BE/LE)
           • UCS-4跟UTF-32其实是一样的编码




2011/6/1                                             7
Coding Modal
• 非Unicode的编码,编码模型比较简单,就是将字符映射
  成一个八位组

• Unicode则建立了一个比较复杂的编码模型,简单的说可
  以分成下面几步:
    – character set - 确认编码包括的所有字符
    – coded character set - 为包括的每一个字符分配一个唯一的正整数,这
      个正整数被称为code points
    – character encoding form - 将每一个code point编码成一个或者若干个
      有限范围的整数值的集合(8位/16位/32位)
    – character encoding scheme - 将有限范围的整数值编码成一个八位组,
      16位和32位整数涉及字节序的问题(LE/BE)
2011/6/1                                                   8
Practices




2011/6/1               9
分类方式
           定长                       变长
8位         ACSII                    GB2312,GBK,GB18030
           ISO 8859 1~10,11,13~16   BIG-5
                                    Shift JIS,EUC-JP
                                    EUC-KR
                                    UTF-8
16位        USC-2                    UTF-16
32位        UTF-32




2011/6/1                                                 10
字串类型
• char*,std::string,    • wchar_t*,std::wstring
  WTF::CString,byte[]     – whar_t是一个类型定义,
                            一般是16位无符号数,不过
    – 代表一个8位定长或者变长          有的编译器/平台的定义为
      编码的字串                 32位无符号数,LE or BE
    – 具体的编码无法通过数据类          由平台本身决定
      型本身获得,需要程序预先        – 如果wchar_t是16位的,
      知道或者从其它途径获得           wchar_t*代表的是一个
                            UCS-2编码的字串(16位定
                            长Unicode)
                          – 如果wchar_t是32位的,
                            wchar_t*代表的是一个
                            UCS-4编码的字串(32位
                            定长Unicode)
2011/6/1                                      11
Cont.
• String(Java),string (.Net),QString(Qt),
  WTF::String(WebKit/WTF)
    – 内部都是采用UTF-16编码(变长16位Unicode), LE or BE由平
      台决定
    – Java/.Net/Qt这样的应用程序框架都内置了编码转换功能,支持
      将其它编码转成UTF-16,或者从UTF-16转换成其它编码
    – WebKit需要第三方库提供编码转换功能,具体使用的库在不同的
      porting版本上可能不一样,一般使用的是ICU,Qt的porting使
      用的是Qt本身的编码转换




2011/6/1                                     12
ASCII兼容性
• 一般其它编码的码表都是             • 进一步说,对于8位的编
  向下兼容ASCII码表,比             码,无论是定长还是变长,
  如说Unicode的码表              一般跟ASCII都是二进制
    – ASCII码表里面的字符的编
                            兼容的
      码值跟该字符在Unicode码      – 假设一个用UTF-8编码的字
      表里面的code point是一样      串,如果里面只包含ASCII
      的                      字符(英文字母,数字,英
                             文标点符号等),你使用
    – 简单说就是假设一个              ASCII编码去转码是完全无
      Unicode的字符,如果它的        错的
      值 <128,其实就可以当作
      一个ASCII字符来处理


2011/6/1                                  13
编码检测
• 对于外部来源的文本数据,比如文本文件,Web页面等,
  我们需要获知它的具体编码,然后再转换成内部的字串类
  型进行进一步处理
    – 如果已知是UTF-16,可以检查头两个字节是否是一个BOM( U+FEFF,在
      UNICODE中代表的意义是ZERO WIDTH NO-BREAK SPACE)来检测编码的字
      节序
           • UTF-16LE以FF FE代表,UTF-16BE以FE FF代表
    – 如果HTTP包,可以检查Header
           • Content-Type: text/html; charset=ISO-8859-4
    – 如果是XML(文件或者页面),可以检查XML声明
           • <?xml version="1.0" encoding="UTF-8" ?>
    – 如果上面的方式都不可用,只能通过某种模式匹配的方式来猜测具体的编码类型,
      或者让用户自己选择编码
2011/6/1                                                   14
The End
           Thank you for your listening
             Yours Sincerely, Roger



2011/6/1                                  15

More Related Content

Viewers also liked

Web Services Hacking and Security
Web Services Hacking and SecurityWeb Services Hacking and Security
Web Services Hacking and SecurityBlueinfy Solutions
 
Brazil 2020: What Brazil will Look Like in the Future
Brazil 2020:  What Brazil will Look Like in the Future Brazil 2020:  What Brazil will Look Like in the Future
Brazil 2020: What Brazil will Look Like in the Future France Houdard
 
XPath - A practical guide
XPath - A practical guideXPath - A practical guide
XPath - A practical guideTobias Schlitt
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scrapingScrapinghub
 
XML & XPath Injections
XML & XPath InjectionsXML & XPath Injections
XML & XPath InjectionsAMol NAik
 

Viewers also liked (6)

Web Services Hacking and Security
Web Services Hacking and SecurityWeb Services Hacking and Security
Web Services Hacking and Security
 
Brazil 2020: What Brazil will Look Like in the Future
Brazil 2020:  What Brazil will Look Like in the Future Brazil 2020:  What Brazil will Look Like in the Future
Brazil 2020: What Brazil will Look Like in the Future
 
Hacking XPATH 2.0
Hacking XPATH 2.0Hacking XPATH 2.0
Hacking XPATH 2.0
 
XPath - A practical guide
XPath - A practical guideXPath - A practical guide
XPath - A practical guide
 
XPath for web scraping
XPath for web scrapingXPath for web scraping
XPath for web scraping
 
XML & XPath Injections
XML & XPath InjectionsXML & XPath Injections
XML & XPath Injections
 

Similar to Character Encoding - Concepts and Practices

Java中编码以及Unicode总结V1.1
Java中编码以及Unicode总结V1.1Java中编码以及Unicode总结V1.1
Java中编码以及Unicode总结V1.1Zianed Hou
 
编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤jay li
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈Xiaozhe Wang
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子Justin Lin
 
第三课 信息编码
第三课 信息编码第三课 信息编码
第三课 信息编码librajin
 
Swift meeting-0718
Swift meeting-0718Swift meeting-0718
Swift meeting-0718Wing Yiu Ng
 
Oracle 数据类型
Oracle 数据类型Oracle 数据类型
Oracle 数据类型yzsind
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectHo Kim
 
[圣思园][Java SE]Io 3
[圣思园][Java SE]Io 3[圣思园][Java SE]Io 3
[圣思园][Java SE]Io 3ArBing Xie
 
Ipv6協定與原理
Ipv6協定與原理Ipv6協定與原理
Ipv6協定與原理dpodp
 
Erlang开发及应用
Erlang开发及应用Erlang开发及应用
Erlang开发及应用litaocheng
 
硬件体系架构浅析
硬件体系架构浅析硬件体系架构浅析
硬件体系架构浅析frogd
 

Similar to Character Encoding - Concepts and Practices (17)

Unicode ncr
Unicode ncrUnicode ncr
Unicode ncr
 
Java中编码以及Unicode总结V1.1
Java中编码以及Unicode总结V1.1Java中编码以及Unicode总结V1.1
Java中编码以及Unicode总结V1.1
 
编码大全 拔赤
编码大全 拔赤编码大全 拔赤
编码大全 拔赤
 
中文编码杂谈
中文编码杂谈中文编码杂谈
中文编码杂谈
 
Encoding
EncodingEncoding
Encoding
 
2. 型態、變數與運算子
2. 型態、變數與運算子2. 型態、變數與運算子
2. 型態、變數與運算子
 
第三课 信息编码
第三课 信息编码第三课 信息编码
第三课 信息编码
 
Swift meeting-0718
Swift meeting-0718Swift meeting-0718
Swift meeting-0718
 
Oracle 数据类型
Oracle 数据类型Oracle 数据类型
Oracle 数据类型
 
编码大全
编码大全编码大全
编码大全
 
Character Encoding and Database Transcoding Project
Character Encoding and Database Transcoding ProjectCharacter Encoding and Database Transcoding Project
Character Encoding and Database Transcoding Project
 
Unicode
UnicodeUnicode
Unicode
 
[圣思园][Java SE]Io 3
[圣思园][Java SE]Io 3[圣思园][Java SE]Io 3
[圣思园][Java SE]Io 3
 
Ipv6協定與原理
Ipv6協定與原理Ipv6協定與原理
Ipv6協定與原理
 
Erlang开发及应用
Erlang开发及应用Erlang开发及应用
Erlang开发及应用
 
09 存储系统01
09 存储系统0109 存储系统01
09 存储系统01
 
硬件体系架构浅析
硬件体系架构浅析硬件体系架构浅析
硬件体系架构浅析
 

More from rogeryi

Why your Android Apps Suck
Why your Android Apps SuckWhy your Android Apps Suck
Why your Android Apps Suckrogeryi
 
Web Page Rendering and Accelerated Compositing
Web Page Rendering and Accelerated CompositingWeb Page Rendering and Accelerated Compositing
Web Page Rendering and Accelerated Compositingrogeryi
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and Morerogeryi
 
Android Hardware Accelerated 2D Rendering
Android Hardware Accelerated 2D RenderingAndroid Hardware Accelerated 2D Rendering
Android Hardware Accelerated 2D Renderingrogeryi
 
Motion and gesture in Android
Motion and gesture in AndroidMotion and gesture in Android
Motion and gesture in Androidrogeryi
 
Voice recognization in Android
Voice recognization in AndroidVoice recognization in Android
Voice recognization in Androidrogeryi
 
Voice Recognization in Android
Voice Recognization in AndroidVoice Recognization in Android
Voice Recognization in Androidrogeryi
 
Layout Management - Android and Qt
Layout Management - Android and QtLayout Management - Android and Qt
Layout Management - Android and Qtrogeryi
 
Java Memory Tips&Tricks
Java Memory Tips&TricksJava Memory Tips&Tricks
Java Memory Tips&Tricksrogeryi
 
Build local web server in 5 minutes with mongoose
Build local web server in 5 minutes with mongooseBuild local web server in 5 minutes with mongoose
Build local web server in 5 minutes with mongooserogeryi
 
Android Event Retrospect
Android Event RetrospectAndroid Event Retrospect
Android Event Retrospectrogeryi
 
Android event retrospect
Android event retrospectAndroid event retrospect
Android event retrospectrogeryi
 
Android Event 02-02-2011 Retrospect
Android Event 02-02-2011 RetrospectAndroid Event 02-02-2011 Retrospect
Android Event 02-02-2011 Retrospectrogeryi
 

More from rogeryi (13)

Why your Android Apps Suck
Why your Android Apps SuckWhy your Android Apps Suck
Why your Android Apps Suck
 
Web Page Rendering and Accelerated Compositing
Web Page Rendering and Accelerated CompositingWeb Page Rendering and Accelerated Compositing
Web Page Rendering and Accelerated Compositing
 
Beyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and MoreBeyond Android Views - Window,Surface,Special Views,and More
Beyond Android Views - Window,Surface,Special Views,and More
 
Android Hardware Accelerated 2D Rendering
Android Hardware Accelerated 2D RenderingAndroid Hardware Accelerated 2D Rendering
Android Hardware Accelerated 2D Rendering
 
Motion and gesture in Android
Motion and gesture in AndroidMotion and gesture in Android
Motion and gesture in Android
 
Voice recognization in Android
Voice recognization in AndroidVoice recognization in Android
Voice recognization in Android
 
Voice Recognization in Android
Voice Recognization in AndroidVoice Recognization in Android
Voice Recognization in Android
 
Layout Management - Android and Qt
Layout Management - Android and QtLayout Management - Android and Qt
Layout Management - Android and Qt
 
Java Memory Tips&Tricks
Java Memory Tips&TricksJava Memory Tips&Tricks
Java Memory Tips&Tricks
 
Build local web server in 5 minutes with mongoose
Build local web server in 5 minutes with mongooseBuild local web server in 5 minutes with mongoose
Build local web server in 5 minutes with mongoose
 
Android Event Retrospect
Android Event RetrospectAndroid Event Retrospect
Android Event Retrospect
 
Android event retrospect
Android event retrospectAndroid event retrospect
Android event retrospect
 
Android Event 02-02-2011 Retrospect
Android Event 02-02-2011 RetrospectAndroid Event 02-02-2011 Retrospect
Android Event 02-02-2011 Retrospect
 

Character Encoding - Concepts and Practices

  • 1. Character Encoding Concepts and Practices Roger yixx@ucweb.com roger2yi@gmail.com www.twitter.com/roger2yi 2011/6/1 1
  • 3. Definition • A character encoding system • 简而言之,字符编码就是 consists of a code that pairs 将字符通过编码转换成另 each character from a given repertoire with something 外一种表现形式,可能是 else, such as a sequence of 自然数的序列,八位组, natural numbers, octets or 电子脉冲等 electrical pulses, in order to – 从上面的定义看,摩斯码, facilitate the transmission of 电报码也是字符编码的一种 data (generally numbers 方式 and/or text) through – 当然我们一般说的字符编码 telecommunication networks 实际上是以八位组的方式编 or storage of text in 码,使用计算机进行显示, computers. 存储和传输 2011/6/1 3
  • 4. Common Character Encodings • 常见的计算机字符编码可以分为3大类 • ASCII及其扩展编码 – ASCII – ISO 8859 1~10 13 ~16(aka Latin编码) – ISO 8859 11 Thai 2011/6/1 4
  • 6. Cont. • CJK(中日韩)东亚语言的编码 – 大陆简体中文的国标编码 — GB2312,GBK,GB18030 – 台湾繁体中文 Big5(大5码) – 日语 Shift JIS, EUC-JP,ISO-2022-JP – 朝鲜语 EUC-KR,ISO-2022-KR 2011/6/1 6
  • 7. Cont. • Unicode编码 – UTF-8 – UTF-16 (BE/LE) • UCS-2 是UTF-16的一个子集,我们在说UTF-16的时候实际包括了 UCS-2,后面会讲到两者的区别 – UTF-32 (BE/LE) • UCS-4跟UTF-32其实是一样的编码 2011/6/1 7
  • 8. Coding Modal • 非Unicode的编码,编码模型比较简单,就是将字符映射 成一个八位组 • Unicode则建立了一个比较复杂的编码模型,简单的说可 以分成下面几步: – character set - 确认编码包括的所有字符 – coded character set - 为包括的每一个字符分配一个唯一的正整数,这 个正整数被称为code points – character encoding form - 将每一个code point编码成一个或者若干个 有限范围的整数值的集合(8位/16位/32位) – character encoding scheme - 将有限范围的整数值编码成一个八位组, 16位和32位整数涉及字节序的问题(LE/BE) 2011/6/1 8
  • 10. 分类方式 定长 变长 8位 ACSII GB2312,GBK,GB18030 ISO 8859 1~10,11,13~16 BIG-5 Shift JIS,EUC-JP EUC-KR UTF-8 16位 USC-2 UTF-16 32位 UTF-32 2011/6/1 10
  • 11. 字串类型 • char*,std::string, • wchar_t*,std::wstring WTF::CString,byte[] – whar_t是一个类型定义, 一般是16位无符号数,不过 – 代表一个8位定长或者变长 有的编译器/平台的定义为 编码的字串 32位无符号数,LE or BE – 具体的编码无法通过数据类 由平台本身决定 型本身获得,需要程序预先 – 如果wchar_t是16位的, 知道或者从其它途径获得 wchar_t*代表的是一个 UCS-2编码的字串(16位定 长Unicode) – 如果wchar_t是32位的, wchar_t*代表的是一个 UCS-4编码的字串(32位 定长Unicode) 2011/6/1 11
  • 12. Cont. • String(Java),string (.Net),QString(Qt), WTF::String(WebKit/WTF) – 内部都是采用UTF-16编码(变长16位Unicode), LE or BE由平 台决定 – Java/.Net/Qt这样的应用程序框架都内置了编码转换功能,支持 将其它编码转成UTF-16,或者从UTF-16转换成其它编码 – WebKit需要第三方库提供编码转换功能,具体使用的库在不同的 porting版本上可能不一样,一般使用的是ICU,Qt的porting使 用的是Qt本身的编码转换 2011/6/1 12
  • 13. ASCII兼容性 • 一般其它编码的码表都是 • 进一步说,对于8位的编 向下兼容ASCII码表,比 码,无论是定长还是变长, 如说Unicode的码表 一般跟ASCII都是二进制 – ASCII码表里面的字符的编 兼容的 码值跟该字符在Unicode码 – 假设一个用UTF-8编码的字 表里面的code point是一样 串,如果里面只包含ASCII 的 字符(英文字母,数字,英 文标点符号等),你使用 – 简单说就是假设一个 ASCII编码去转码是完全无 Unicode的字符,如果它的 错的 值 <128,其实就可以当作 一个ASCII字符来处理 2011/6/1 13
  • 14. 编码检测 • 对于外部来源的文本数据,比如文本文件,Web页面等, 我们需要获知它的具体编码,然后再转换成内部的字串类 型进行进一步处理 – 如果已知是UTF-16,可以检查头两个字节是否是一个BOM( U+FEFF,在 UNICODE中代表的意义是ZERO WIDTH NO-BREAK SPACE)来检测编码的字 节序 • UTF-16LE以FF FE代表,UTF-16BE以FE FF代表 – 如果HTTP包,可以检查Header • Content-Type: text/html; charset=ISO-8859-4 – 如果是XML(文件或者页面),可以检查XML声明 • <?xml version="1.0" encoding="UTF-8" ?> – 如果上面的方式都不可用,只能通过某种模式匹配的方式来猜测具体的编码类型, 或者让用户自己选择编码 2011/6/1 14
  • 15. The End Thank you for your listening Yours Sincerely, Roger 2011/6/1 15