??xml version="1.0" encoding="utf-8" standalone="yes"?>国产精品视频区,国产精品三级在线观看,www.欧美http://www.aygfsteel.com/youthyflyer/category/1349.html在Eclipse的vz中一口接一口的喝水~~zh-cnFri, 02 Mar 2007 23:16:20 GMTFri, 02 Mar 2007 23:16:20 GMT60[转蝲]谈谈Unicode~码Q简要解释UCS、UTF、BMP、BOM{名?/title><link>http://www.aygfsteel.com/youthyflyer/articles/4338.html</link><dc:creator>youthyflyer</dc:creator><author>youthyflyer</author><pubDate>Mon, 16 May 2005 07:02:00 GMT</pubDate><guid>http://www.aygfsteel.com/youthyflyer/articles/4338.html</guid><wfw:comment>http://www.aygfsteel.com/youthyflyer/comments/4338.html</wfw:comment><comments>http://www.aygfsteel.com/youthyflyer/articles/4338.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.aygfsteel.com/youthyflyer/comments/commentRss/4338.html</wfw:commentRss><trackback:ping>http://www.aygfsteel.com/youthyflyer/services/trackbacks/4338.html</trackback:ping><description><![CDATA[<SPAN id=ArticleContent1_ArticleContent1_lblContent><A ><FONT color=#800080 size=2>选择?fmddlmyy ?Blog</FONT> <BR></A> <P><FONT size=2><BR>q是一程序员写给E序员的味ȝ。所谓趣x指可以比较轻村֜了解一些原来不清楚的概念,增进知识Q类g打RPG游戏的升U。整理这文章的动机是两个问题:</FONT></P> <DL> <DT><FONT size=2>问题一Q?</FONT> <DD> <P><FONT size=2>使用WindowsC本的“另存ؓ”,可以在GBK、Unicode、Unicode big endian和UTF-8q几U编码方式间怺转换。同htxt文gQWindows是怎样识别~码方式的呢Q?/FONT></P> <P><FONT size=2>我很早前发现Unicode、Unicode big endian和UTF-8~码的txt文g的开头会多出几个字节Q分别是FF、FEQUnicodeQ?FE、FFQUnicode big endianQ?EF、BB、BFQUTF-8Q。但q些标记是基于什么标准呢Q?/FONT></P> <DT><FONT size=2>问题二: </FONT> <DD><FONT size=2>最q在|上看到一个ConvertUTF.cQ实CUTF-32、UTF-16和UTF-8q三U编码方式的怺转换。对于Unicode(UCS2)、GBK、UTF-8q些~码方式Q我原来׃解。但q个E序让我有些p涂Q想不v来UTF-16和UCS2有什么关pR?</FONT></DD></DL> <P><FONT size=2>查了查相兌料,ȝ这些问题弄清楚了,带也了解了一些Unicode的细节。写成一文章,送给有过cM疑问的朋友。本文在写作时尽量做到通俗易懂Q但要求读者知道什么是字节Q什么是十六q制?/FONT></P> <H3>0、big endian和little endian</H3> <P><FONT size=2>big endian和little endian是CPU处理多字节数的不同方式。例如“汉”字的Unicode~码?C49。那么写到文仉ӞI竟是将6C写在前面Q还是将49写在前面Q如果将6C写在前面Q就是big endian。还是将49写在前面Q就是little endian?/FONT></P> <P><FONT size=2>“endian”这个词《格列佛游记》。小人国的内战就源于吃鸡蛋时是究竟从大头(Big-Endian)敲开q是从小?Little-Endian)敲开Q由此曾发生q六ơ叛乱,其中一个皇帝送了命,另一个丢了王位?/FONT></P> <P><FONT size=2>我们一般将endian译成“字节序”,big endian和little endianUC“大䏀和“小䏀?/FONT></P> <H3>1、字W编码、内码,带介绍汉字~码</H3> <P><FONT size=2>字符必须~码后才能被计算机处理。计机使用的缺省编码方式就是计机的内码。早期的计算Z?位的ASCII~码Qؓ了处理汉字,E序员设计了用于体中文的GB2312和用于繁体中文的big5?/FONT></P> <P><FONT size=2>GB2312(1980q?一共收录了7445个字W,包括6763个汉字和682个其它符受汉字区的内码范围高字节从B0-F7Q低字节从A1-FEQ占用的码位?2*94=6768。其中有5个空位是D7FA-D7FE?/FONT></P> <P><FONT size=2>GB2312支持的汉字太?995q的汉字扩展规范GBK1.0收录?1886个符P它分为汉字区和图形符号区。汉字区包括21003个字W?000q的GB18030是取代GBK1.0的正式国家标准。该标准收录?7484个汉字,同时q收录了藏文、蒙文、维向ְ文等主要的少数民族文字。现在的PCq_必须支持GB18030Q对嵌入式品暂不作要求。所以手机、MP3一般只支持GB2312?/FONT></P> <P><FONT size=2>从ASCII、GB2312、GBK到GB18030Q这些编码方法是向下兼容的,卛_一个字W在q些Ҏ中L有相同的~码Q后面的标准支持更多的字W。在q些~码中,英文和中文可以统一地处理。区分中文编码的Ҏ是高字节的最高位不ؓ0。按照程序员的称|GB2312、GBK到GB18030都属于双字节字符?(DBCS)?/FONT></P> <P><FONT size=2>有的中文Windows的缺省内码还是GBKQ可以通过GB18030升包升U到GB18030。不qGB18030相对GBK增加的字W,普通h是很隄到的Q通常我们q是用GBK指代中文Windows内码?/FONT></P> <P><FONT size=2>q里q有一些细节:</FONT></P> <UL> <LI> <P><FONT size=2>GB2312的原文还是区位码Q从Z码到内码Q需要在高字节和低字节上分别加上A0?/FONT></P> <LI> <P><FONT size=2>在DBCS中,GB内码的存储格式始l是big endianQ即高位在前?/FONT></P> <LI> <P><FONT size=2>GB2312的两个字节的最高位都是1。但W合q个条g的码位只?28*128=16384个。所以GBK和GB18030的低字节最高位都可能不?。不q这不媄响DBCS字符的解析Q在dDBCS字符时Q只要遇到高位ؓ1的字节,可以将下两个字节作Z个双字节~码Q而不用管低字节的高位是什么?/FONT></P></LI></UL> <H3>2、Unicode、UCS和UTF</H3> <P><FONT size=2>前面提到从ASCII、GB2312、GBK到GB18030的编码方法是向下兼容的。而Unicode只与ASCII兼容Q更准确地说Q是与ISO-8859-1兼容Q,与GB码不兼容。例如“汉”字的Unicode~码?C49Q而GB码是BABA?/FONT></P> <P><FONT size=2>Unicode也是一U字W编码方法,不过它是由国际组l设计,可以容纳全世界所有语a文字的编码方案。Unicode的学名是"Universal Multiple-Octet Coded Character Set"Q简UCؓUCS。UCS可以看作?Unicode Character Set"的羃写?/FONT></P> <P><FONT size=2>Ҏl基癄全书(http://zh.wikipedia.org/wiki/)的记载:历史上存在两个试囄立设计Unicode的组l,卛_际标准化l织QISOQ和一个Y件制造商的协会(unicode.orgQ。ISO开发了ISO 10646目QUnicode协会开发了Unicode目?/FONT></P> <P><FONT size=2>?991q前后,双方都认识到世界不需要两个不兼容的字W集。于是它们开始合q双方的工作成果Qƈ为创立一个单一~码表而协同工作。从Unicode2.0开始,Unicode目采用了与ISO 10646-1相同的字库和字码?/FONT></P> <P><FONT size=2>目前两个目仍都存在Qƈ独立地公布各自的标准。Unicode协会现在的最新版本是2005q的Unicode 4.1.0。ISO的最新标准是10646-3:2003?/FONT></P> <P><FONT size=2>UCS规定了怎么用多个字节表C各U文字。怎样传输q些~码Q是由UTF(UCS Transformation Format)规范规定的,常见的UTF规范包括UTF-8、UTF-7、UTF-16?/FONT></P> <P><FONT size=2>IETF的RFC2781和RFC3629以RFC的一贯风|清晰、明快又不失严}地描qCUTF-16和UTF-8的编码方法。我LC得IETF是Internet Engineering Task Force的羃写。但IETF负责l护的RFC是Internet上一切规范的基础?/FONT></P> <H3>3、UCS-2、UCS-4、BMP</H3> <P></P> <P><FONT size=2>UCS有两U格式:UCS-2和UCS-4。顾名思义QUCS-2是用两个字节编码,UCS-4是?个字节(实际上只用了31位,最高位必须?Q编码。下面让我们做一些简单的数学游戏Q?/FONT></P> <P><FONT size=2>UCS-2?^16=65536个码位,UCS-4?^31=2147483648个码位?/FONT></P> <P><FONT size=2>UCS-4Ҏ最高位?的最高字节分?^7=128个group。每个group再根据次高字节分?56个plane。每个planeҎW?个字节分?56?(rows)Q每行包?56个cells。当然同一行的cells只是最后一个字节不同,其余都相同?/FONT></P> <P><FONT size=2>group 0的plane 0被称作Basic Multilingual Plane, 即BMP。或者说UCS-4中,高两个字节ؓ0的码位被UCBMP?/FONT></P> <P><FONT size=2>UCS-4的BMPL前面的两个零字节得CUCS-2。在UCS-2的两个字节前加上两个零字节,得CUCS-4的BMP。而目前的UCS-4规范中还没有M字符被分配在BMP之外?/FONT></P> <H3>4、UTF~码</H3> <P></P> <P><FONT size=2>UTF-8是?位ؓ单元对UCSq行~码。从UCS-2到UTF-8的编码方式如下:</FONT></P> <TABLE width="75%" border=1> <TBODY> <TR> <TD><FONT size=2>UCS-2~码(16q制)</FONT></TD> <TD><FONT size=2>UTF-8 字节?二进?</FONT></TD></TR> <TR> <TD><FONT size=2>0000 - 007F</FONT></TD> <TD><FONT size=2>0xxxxxxx</FONT></TD></TR> <TR> <TD><FONT size=2>0080 - 07FF</FONT></TD> <TD><FONT size=2>110xxxxx 10xxxxxx</FONT></TD></TR> <TR> <TD><FONT size=2>0800 - FFFF</FONT></TD> <TD><FONT size=2>1110xxxx 10xxxxxx 10xxxxxx</FONT></TD></TR></TBODY></TABLE> <P><FONT size=2>例如“汉”字的Unicode~码?C49?C49?800-FFFF之间Q所以肯定要?字节模板了:<FONT color=#0000ff>1110</FONT>xxxx <FONT color=#0000ff>10</FONT>xxxxxx <FONT color=#0000ff>10</FONT>xxxxxx。将6C49写成二进制是Q?110 110001 001001Q?用这个比Ҏ依次代替模板中的xQ得刎ͼ<FONT color=#0000ff>1110</FONT>0110 <FONT color=#0000ff>10</FONT>110001 <FONT color=#0000ff>10</FONT>001001Q即E6 B1 89?/FONT></P> <P><FONT size=2>读者可以用C本测试一下我们的~码是否正确?/FONT></P> <P><FONT size=2>UTF-16?6位ؓ单元对UCSq行~码。对于小?x10000的UCS码,UTF-16~码q于UCS码对应的16位无W号整数。对于不于0x10000的UCS码,定义了一个算法。不q由于实际用的UCS2Q或者UCS4的BMP必然于0x10000Q所以就目前而言Q可以认为UTF-16和UCS-2基本相同。但UCS-2只是一个编码方案,UTF-16却要用于实际的传输,所以就不得不考虑字节序的问题?/FONT></P> <H3>5、UTF的字节序和BOM</H3> <P><FONT size=2>UTF-8以字节ؓ~码单元Q没有字节序的问题。UTF-16以两个字节ؓ~码单元Q在解释一个UTF-16文本前,首先要弄清楚每个~码单元的字节序。例如收C个“奎”的Unicode~码?94EQ“乙”的Unicode~码?E59。如果我们收到UTF-16字节?94E”,那么q是“奎”还是“乙”?</FONT></P> <P><FONT size=2>Unicode规范中推荐的标记字节序的方法是BOM。BOM不是“Bill Of Material”的BOM表,而是Byte Order Mark。BOM是一个有点小聪明的想法:</FONT></P> <P><FONT size=2>在UCS~码中有一个叫?ZERO WIDTH NO-BREAK SPACE"的字W,它的~码是FEFF。而FFFE在UCS中是不存在的字符Q所以不应该出现在实际传输中。UCS规范我们在传输字节流前,先传输字W?ZERO WIDTH NO-BREAK SPACE"?/FONT></P> <P><FONT size=2>q样如果接收者收到FEFFQ就表明q个字节是Big-Endian的;如果收到FFFEQ就表明q个字节是Little-Endian的。因此字W?ZERO WIDTH NO-BREAK SPACE"又被UCBOM?/FONT></P> <P><FONT size=2>UTF-8不需要BOM来表明字节顺序,但可以用BOM来表明编码方式。字W?ZERO WIDTH NO-BREAK SPACE"的UTF-8~码是EF BB BFQ读者可以用我们前面介绍的编码方法验证一下)。所以如果接收者收CEF BB BF开头的字节,q道这是UTF-8~码了?/FONT></P> <P><FONT size=2>Windows是使用BOM来标记文本文件的~码方式的?/FONT></P> <H3>6、进一步的参考资?/H3> <P><FONT size=2>本文主要参考的资料?"Short overview of ISO-IEC 10646 and Unicode" (http://www.nada.kth.se/i18n/ucs/unicode-iso10646-oview.html)?/FONT></P> <P><FONT size=2>我还找了两篇看上M错的资料Q不q因为我开始的疑问都找C{案Q所以就没有看:</FONT></P> <OL> <LI><FONT size=2>"Understanding Unicode A general introduction to the Unicode Standard" (http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&item_id=IWS-Chapter04a) </FONT> <LI><FONT size=2>"Character set encoding basics Understanding character set encodings and legacy encodings" (http://scripts.sil.org/cms/scripts/page.php?site_id=nrsi&item_id=IWS-Chapter03) </FONT></LI></OL> <P><FONT size=2>希望有读者能从中受益?BR><BR>原文地址Q?A >http://dev.csdn.net/develop/article/69/article/69/69883.shtm</A></FONT></P></SPAN><img src ="http://www.aygfsteel.com/youthyflyer/aggbug/4338.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.aygfsteel.com/youthyflyer/" target="_blank">youthyflyer</a> 2005-05-16 15:02 <a href="http://www.aygfsteel.com/youthyflyer/articles/4338.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss> <footer> <div class="friendship-link"> <a href="http://www.aygfsteel.com/" title="狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频">狠狠久久亚洲欧美专区_中文字幕亚洲综合久久202_国产精品亚洲第五区在线_日本免费网站视频</a> </div> </footer> վ֩ģ壺 <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɣ</a>| <a href="http://" target="_blank">Դ</a>| <a href="http://" target="_blank">ͷ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ľ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Խ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">̨</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Ӳ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">÷ӿ</a>| <a href="http://" target="_blank">ɽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ƽ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ˮ</a>| <a href="http://" target="_blank">ƽԶ</a>| <a href="http://" target="_blank">³</a>| <a href="http://" target="_blank">Ƹ</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">Т</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank">ʯɽ</a>| <a href="http://" target="_blank">߶</a>| <a href="http://" target="_blank"></a>| <a href="http://" target="_blank"></a>| <script> (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https') { bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else { bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })(); </script> </body>