前面写过一篇关于ucs2与utf8转换的东西,关于utf8字节数范围有些不太准确的描述。网上查找了下资料基本也没有完全描述清楚(无论是说3、4、6字节都不能完全算错)。个人觉得写指导性的文章需要对内容负责,并且技术性研究应该严谨,不应该出现模棱两可的情况。正好这两天不太忙,就继续查了查资料想确定utf8占用字节数准确的描述。理了理unicode、ucs2及utf8之间的种种。
因此以utf8究竟字节范围多少为线索,来说说他们之间的一些关系
UNICODE --by Unicode Technical
Committee
http://www.rfc-editor.org/rfc/rfc5198.txt
Unicode identifies each character by an integer, called its "code
point", in the range 0-0x10ffff. en…UNICODE是字符集,一个以整数为“码位索引”的巨大映射表,范围为0x000000-0x10ffff.
强调一下是映射表,每个字符一个“码位”对应。
UCS
--by ISO
UCS是Universal Multiple-Octet Coded Character
Set的缩写,并且是由ISO制定。
UCS2(Universal Character Set coded in 2 octets)
即UCS2用2个字节标识“码位”,表示UNICODE
http://tieba.baidu.com/f?kz=84127015
ISO10646的第0群组第0字面(群八位元和面八位元的值都为00h) 称为「基本多语文字面」(Basic Multilingual
Plane, BMP)0x00-0xFF,
其编码字元与Unicode相同。BMP之外的32,767个字面区分为辅助字面(supplementary
planes)和专用字面(private use
planes)。当电脑系统只使用BMP的字元码时,可以省略群八位元和面八位元,因而而将字元码由32个位元缩短为16个位元,称为ISO10646字元码的基本面形式(可简称为UCS-2),其实也可视同於Unicode(0x00-0xFF范围内)。
简单的说UCS2就是2个字节(16位元)表示Unicode。
继续强调一下是映射表,每个字符一个“码位”对应。
UCS4
类似UCS2..UCS4就是4字节(32位元)表示Unicode。
综合看UCS与UNICODE的关系为2个标准组织出了2套标准,但是都觉得没有必要搞2套完全独立的字符标准,所以他们兼容了。在0x00-0xFF这个区间他们是完全一样的(UCS2)。
UTF
(UCS
Transformation Format),注意是Transformation
format,是作为计算机用的编码方式。
因此会涉及到字节序的问题。即大头序(Big Endian)、小头序(Little
Endian)问题,如UTF8LE,UTF8BE,UTF16LE,UTF16BE等。
我个人的理解为以编码方式来表现UNICODE字符集。
UTF8
http://www.ietf.org/rfc/rfc2279.txt
UTF-8 definition
In UTF-8, characters are encoded using sequences of 1
to 6 octets.
The only octet of a "sequence" of one has the
higher-order bit set to
0, the remaining 7 bits being used to encode the
character value. In
a sequence of n octets, n>1, the
initial octet has the n higher-order
bits set to 1, followed by a bit set to 0. The
remaining bit(s) of
that octet contain bits from the value of the
character to be
encoded. The following octet(s) all have the
higher-order bit set to
1 and the following bit set to 0, leaving 6 bits in
each to contain
bits from the character to be encoded.
The table below summarizes the format of these
different octet types.
The letter x indicates bits available for encoding
bits of the UCS-4
character value.
UCS-4 range
(hex.) UTF-8 octet sequence (binary)
0000 0000-0000 007F 0xxxxxxx
0000 0080-0000 07FF 110xxxxx
10xxxxxx
0000 0800-0000 FFFF 1110xxxx 10xxxxxx
10xxxxxx
0001 0000-001F FFFF 11110xxx 10xxxxxx
10xxxxxx 10xxxxxx
0020 0000-03FF FFFF 111110xx 10xxxxxx
10xxxxxx 10xxxxxx 10xxxxxx
0400 0000-7FFF FFFF 1111110x 10xxxxxx ...
10xxxxxx
UTF8是采用8bits编码,字节数范围为1-6字节(嘿嘿真相大白)。
那么看看为什么网上还有不少资料显示字节范围为3或4呢。
其实是编码表示的对应字符集映射表不同导致。
所以真相是,UTF8这种编码方式的字节数范围为1-6字节。
在UCS2转换为UTF8时,由于单个UCS2字符的范围在0x00-0xFFFF.转换为UTF8则最大只可能为3字节。
而UNICODE的范围在0x000000-0x10FFFF,对应与0001 0000-001F
FFFF区间,转换为UTF8则最大只能为4字节。
题外话:有人说中文字符在UTF8不会有4字节的编码。那么可以试试下面link里的那个字,很不幸我的XP系统无法正常显示这个字(除浏览器、MSN外),Win7的同事则可以。应该是在本机使用的字符集编码外.
http://bbs.zdic.net/redirect.php?tid=120293&goto=lastpost
UTF16
http://www.rfc-editor.org/rfc/rfc2781.txt
类似UTF8,UTF16是采用16bits编码(双字节).
但需要注意UTF16所能表示的UCS4区间为0x0000 0000-0x0010 FFFF.
不考虑字节序的情况下,所有的UCS2都可以视作UTF16的编码。