Character Encoding - Concepts and Practices

15
Character Encoding Concepts and Practices Roger [email protected] [email protected] www.twitter.com/roger2yi 2011/6/1 1

description

介绍字符编码的一些基本概念和实践中的使用

Transcript of Character Encoding - Concepts and Practices

Page 1: Character Encoding - Concepts and Practices

Character EncodingConcepts and [email protected]@gmail.comwww.twitter.com/roger2yi

2011/6/1 1

Page 2: Character Encoding - Concepts and Practices

Concepts

2011/6/1 2

Page 3: Character Encoding - Concepts and Practices

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

Page 4: Character Encoding - Concepts and Practices

Common Character Encodings

• 常见的计算机字符编码可以分为3大类

• ASCII及其扩展编码

– ASCII

– ISO 8859 1~10 13 ~16(aka Latin编码)

– ISO 8859 11 Thai

2011/6/1 4

Page 5: Character Encoding - Concepts and Practices

Cont.

2011/6/1 5

Page 6: Character Encoding - Concepts and Practices

Cont.

• CJK(中日韩)东亚语言的编码

– 大陆简体中文的国标编码 — GB2312,GBK,GB18030

– 台湾繁体中文 Big5(大5码)

– 日语 Shift JIS, EUC-JP,ISO-2022-JP

– 朝鲜语 EUC-KR,ISO-2022-KR

2011/6/1 6

Page 7: Character Encoding - Concepts and Practices

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

Page 8: Character Encoding - Concepts and Practices

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

Page 9: Character Encoding - Concepts and Practices

Practices

2011/6/1 9

Page 10: Character Encoding - Concepts and Practices

分类方式

定长 变长8位 ACSII

ISO 8859 1~10,11,13~16GB2312,GBK,GB18030BIG-5Shift JIS,EUC-JPEUC-KRUTF-8

16位 USC-2 UTF-1632位 UTF-32

2011/6/1 10

Page 11: Character Encoding - Concepts and Practices

字串类型

• char*,std::string,WTF::CString,byte[]– 代表一个8位定长或者变长编码的字串

– 具体的编码无法通过数据类型本身获得,需要程序预先知道或者从其它途径获得

• wchar_t*,std::wstring– whar_t是一个类型定义,一般是16位无符号数,不过有的编译器/平台的定义为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

Page 12: Character Encoding - Concepts and Practices

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

Page 13: Character Encoding - Concepts and Practices

ASCII兼容性

• 一般其它编码的码表都是向下兼容ASCII码表,比如说Unicode的码表– ASCII码表里面的字符的编码值跟该字符在Unicode码表里面的code point是一样的

– 简单说就是假设一个Unicode的字符,如果它的值 <128,其实就可以当作一个ASCII字符来处理

• 进一步说,对于8位的编码,无论是定长还是变长,一般跟ASCII都是二进制兼容的– 假设一个用UTF-8编码的字串,如果里面只包含ASCII字符(英文字母,数字,英文标点符号等),你使用ASCII编码去转码是完全无错的

2011/6/1 13

Page 14: Character Encoding - Concepts and Practices

编码检测

• 对于外部来源的文本数据,比如文本文件,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

Page 15: Character Encoding - Concepts and Practices

The End

Thank you for your listeningYours Sincerely, Roger

2011/6/1 15