您的位置:首页 > 其它

unicode、UTF-8、UTF-16、UTF-32、code point、code unit、Byte Order Mark(BOM)

2013-04-16 13:19 525 查看
最近看到有关于code point和code unit的书,看得很模糊就Google了一下,看到一篇很不错的文章,写的非常详细(除了UTF-16的部分内容我没有看懂,但是意思领会了)。现在就我的理解做一些简单的说明,具体内容请查阅原文

字符集就是字符的集合,编码字符集是一个字符集,它为每一个字符分配一个唯一的数字。代码点是指可用于编码字符集的数字。字符编码方案是从一个或多个编码字符集到一个或多个不定宽度代码单元序列的映射。最常用的unicode编码方案是,UTF-8、UTF-16、UTF-32。举一个简单的例子:

1、假如现在有10个大一新生入学,那么10个大一新生就可以看成是有10个字符的字符集;

2、现在要唯一的标识这是个学生,就要给这10个大一新生安排学号,学号满足2013***的模式,其中*代表可以是0-9的任何数字,那么现在这个学号模式20113***,可以代表10*10*10个数字,从2013000-2013999,这就相当于有10*10*10的代码点,二者10*10*10个代码点可以编码(代表)10*10*10个学生,但是现在实际上只有10个学生,也就是只是用2013000-20130009这十个数字,安排的学号只是学生的另一个名称(其中一个就是你的名字,但是名字可能会重复,但是学号不会),没有什么其他特殊的含义;

3、现在要考试了,我们一般情况下都会在试卷上写上自己的学号,这个时候,一般情况下,我们会这样写:2013001,但是,现在老师给了我们一些权力,可以按照老师定义的一些格式写自己的学号,这些格式可能是:0012013(后三位与代表入学年份的数字对调),但是必须保证从2013001到*******(代表老师允许的学号书写格式中2013001对应的数字)之间的映射关系是1:1,而老师定义的书写学号的特定的格式就是所谓的UTF-8、UTF-16、UTF-32了。

===============2013/11/01================

代码点 代码单元:citation:http://www.cnblogs.com/zhangzl419/archive/2013/05/21/3090601.html

以前看《Java核心技术 卷I》,看到了3.6.5节 代码点和代码单元,看了几遍但是没有彻底明白。直达最近发现一篇网络文章:https://github.com/acmerfight/insight_python/blob/master/Unicode_and_Character_Sets.md,
其中对编码历史进行了回顾,指出了代码点和代码单元出现的原因,让我对代码点和代码单元有了清晰的理解。


代码点(Code Point):Unicode是属于编码字符集(CCS)的范围。Unicode所做的事情就是将我们需要表示的字符表中的每个字符映射成一个数字,这个数字被称为相应字符的码点(code point)。例如“严”字在Unicode中对应的码点是U+0x4E25。
代码点是字符集被编码后出现的概念。字符集(Code Set)是一个集合,集合中的元素就是字符,比如ASCII字符集,其中的字符就是'A'、'B'等字符。为了在计算机中处理字符集,必须把字符集数字化,就是给字符集中的每一个字符一个编号,计算机程序中要用字符,直接用这个编号就可以了。于是就出现了编码后的字符集,叫做编码字符集(Coded
Code Set)。编码字符集中每一个字符都和一个编号对应。那么这个编号就是代码点(Code Point)。
代码单元(Code Unit):是指一个已编码的文本中具有最短的比特组合的单元。对于UTF-8来说,码元是8比特长;对于UTF-16来说,码元是16比特长。换一种说法就是UTF-8的是以一个字节为最小单位的,UTF-16是以两个字节为最小单位的。
代码单元是把代码点存放到计算机后出现的概念。一个字符集,比如有10个字符,每一个字符从0到9依次编码。那么代码点就是0、1、。。。、9。为了在计算机中存储这10个代代码点,一个代码点给一个字节,那么这里的一个字节就是一个代码单元。比如Unicode是一个编码字符集,其中有65536个字符,代码点依次为0、1、2、。。。、65535,为了在计算机中表示这些代码点就出现了代码单元,65536个代码点为了统一表示每个代码点必须要有两个字节表示才行。但是为了节省空间0-127的ASCII码就可以不用两个字节来表示,只需要一个字节,于是不同的表示方案就形成了不同的编码方案,比如utf-8、utf-16等。对utf-8而言代码单元就是一个字节,对utf-16而言代码单元就是两个字节。
==============2013/11/04==================
Unicode glossary:http://www.unicode.org/glossary/
code unit : The minimal bit combination that can represent a unit of encoded text for
processing or interchange. The Unicode Standard uses 8-bit code units in the UTF-8 encoding form, 16-bit code units in the UTF-16 encoding form, and 32-bit code units in the UTF-32 encoding form.
code point : (1) Any value
in the Unicode codespace; that is, the range of integers from 0 to 10FFFF16.Not all code points are assigned to encoded characters.(2)
A value, or position, for a character, in any coded character set.
citation:Oracle
doc
The
Java platform uses the UTF-16 representation in
char
arrays and in the
String
and
StringBuffer
classes.

citation 1:http://msdn.microsoft.com/en-us/library/ms225454(v=vs.80).aspx
Code points and code units
In
each encoding, the code points are mapped to one or more code units.

A "code unit" is a single unit within
each encoding form. The code unit size is equivalent to the bit measurement for the particular encoding:

1、A code unit in UTF-8 consists of 8
bits.

2、A code unit in UTF-16 consists of
16 bits.

3、A code unit in UTF-32 consists of
32 bits.

4、In GB18030, a code unit consists of
8 bits.

Number of code units in each code point

The number of code units required to
be mapped to a code point varies across encoding forms:
1、UTF-8

Multiple
code units per code point are common in UTF-8 because of the smaller code units. The code points will be mapped to one, two, three, or four code units.
2、UTF-16

UTF-16
code units are twice as large as 8-bit code units. Therefore, any code points with a scalar value less than U+10000 is encoded with a single code unit.

For code points with a scalar value of U+10000 or higher, two code units are required per code point. These
pairs of code units have a unique term in UTF-16: "Unicode surrogate pairs".

3、UTF-32

The 32-bit code unit used in UTF-32 is large enough that every code point is encoded as a single code unit.

4、GB18030

Multiple code units per code point are common in GB18030 because of the smaller code units. The code points will be mapped to one, two, or four code
units.

citation 2:What
is a Unicode code unit and a Unicode code point?
citation 3:Unicode
4.0 support in J2SE 1.5
citation 4:counting
Characters
Character:a character can usefully
be defined as the smallest atomic unit of text with semantic value.

citation 5:
on the goodness ofUnicode
citation 6:comparinga
char to a code-point?
citation 7:The
Absolute MinimumEvery Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)

It does not make sense to have a string without knowing what encoding it uses.If you have a string, in memory, in a file, or in an email message, you
have to know what encoding it is in or you cannot interpret it or display it to users correctly.

=================2013/11/06===================

BOM,byte order mark

citation 8: 维基百科

citation 9: UTF-8文件的Unicode签名BOM(Byte
Order Mark)问题

=================2014/03/27===============

citation 10: Regex Tutorial
- Unicode Characters and Properties



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: