⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 对utf8编码的初步认识.htm

📁 对UTF8编码的初步认识
💻 HTM
字号:
<html>

<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<title>对UTF8编码的初步认识</title>
</head>

<body>

<p align="center"><b><span id="ArticleTitle1_ArticleTitle1_lblTitle">
对UTF8编码的初步认识</span></b></p>
<form id="Form1" name="Form1" method="post" action="http://dev.csdn.net/article/40/readarticle.aspx?id=40146">
	<span id="ArticleContent1_ArticleContent1_lblContent">
	在网络中有很多地方都有采用UTF8编码,由于要编写与邮件服务端有关的程序,而邮件服务端有些地方用到了UTF8编码,所以对它有了初步的认识!<br>
	<br>
	它其实和Unicode是同类,就是在编码方式上不同!<br>
	首先UTF8编码后的大小是不一定,不像Unicode编码后的大小是一样的!<br>
	<br>
	我们先来看Unicode的编码:一个英文字母 “a” 和 一个汉字 “好”,编码后都是占用的空间大小是一样的,都是两个字节!<br>
	<br>
	而UTF8编码:一个英文字母“a” 和 一个汉字 “好”,编码后占用的空间大小就不样了,前者是一个字节,后者是三个字节!<br>
	<br>
	现在就让我们来看看UTF8编码的原理吧:<br>
	  因为一个字母还有一些键盘上的符号加起来只用二进制七位就可以表示出来,而一个字节就是八位,所以UTF8就用一个字节来表式字母和一些键盘上的符号。然而当我们拿到被编码后的一个字节后怎么知道它的组成?它有可能是英文字母的一个字节,也有可能是汉字的三个字节中的一个字节!所以,UTF8是有标志位的!<br>
	<br>
	  当要表示的内容是 7位 的时候就用一个字节:0*******&nbsp;  第一个0为标志位,剩下的空间正好可以表示ASCII 0-127 的内容。<br>
	<br>
	  当要表示的内容在 8 到 11 位的时候就用两个字节:110***** 10******&nbsp;  第一个字节的110和第二个字节的10为标志位。<br>
	<br>
	  当要表示的内容在 12 到 16 位的时候就用三个字节:1110***** 10****** 10****** 
	   和上面一样,第一个字节的1110和第二、三个字节的10都是标志位,剩下的空间正好可以表示汉字。<br>
	<br>
	  以此类推:<br>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 四个字节:11110**** 10****** 10****** 10****** <br>
	  五个字节:111110*** 10****** 10****** 10****** 10******&nbsp;<br>
	  六个字节:1111110** 10****** 10****** 10****** 10****** 10******&nbsp;<br>
	  .............................................<br>
	&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; ..............................................<br>
	<br>
	明白了没有?<br>
	编码的方法是从低位到高位<br>
	<br>
	现在就让我们来看看实例吧!<br>
	<br>
	&nbsp;黄色为标志位<br>
	其它着色为了显示其,编码后的位置 

	<table style="width: 765px; height: 138px" height="138" cellSpacing="0" cellPadding="0" width="765" border="1" id="table1">
		<tr>
			<td>
			<p align="center">Unicode十六进制</td>
			<td>
			<p align="center">Unicode二进制</td>
			<td>
			<p align="center">UTF8二进制</td>
			<td>
			<p align="center">UTF8十六进制</td>
			<td>
			<p align="center">UTF8字节数</td>
		</tr>
		<tr>
			<td>
			<p align="center">B</td>
			<td>
			<p align="center"><font style="background-color: #ffc0cb">00001011</font></td>
			<td>
			<p align="center"><font style="background-color: #ffff00">0</font><font style="background-color: #ffc0cb">0001010</font></td>
			<td>
			<p align="center">B</td>
			<td>
			<p align="center">1</td>
		</tr>
		<tr>
			<td>
			<p align="center">9D</td>
			<td>
			<p align="center"><font style="background-color: #ffc0cb">00010</font><font style="background-color: #808080">011101</font></td>
			<td>
			<p align="center"><font style="background-color: #ffff00">110</font><font style="background-color: #ffc0cb">00010</font>
			<font style="background-color: #ffff00">10</font><font style="background-color: #808080">011101
			</font></td>
			<td>
			<p align="center">C2 9D</td>
			<td>
			<p align="center">2</td>
		</tr>
		<tr>
			<td>
			<p align="center">A89E</td>
			<td>
			<p align="center"><font style="background-color: #ffc0cb">1010</font><font style="background-color: #808080">1000 
			10</font><font style="background-color: #7fffd4">011110</font></td>
			<td>
			<p align="center"><font style="background-color: #ffff00">1110</font><font style="background-color: #ffc0cb">1010</font>&nbsp;<font style="background-color: #ffff00">10</font><font style="background-color: #808080">100010</font>
			<font style="background-color: #ffff00">10</font><font style="background-color: #7fffd4">011110</font></td>
			<td>
			<p align="center">EA A2 9E</td>
			<td>
			<p align="center">3</td>
		</tr>
	</table>
	</span><br>
 <h2>关于Utf8编码的几个函数 </h2>
	<div class="postbody">
		<font face="Courier New">最近一段时间老弄Utf8编码,工作时写了几个函数,给大家指正一下<br>
		<br>
		//////////////////////////////////////////////<br>
		//---------取得utf8字符的长度---------------//<br>
		//Str:String 源字符串<br>
		//Result:Integer utf8字符串长度<br>
		class function TPduPush.getUTF8Len(Str: string): Integer;<br>
		var<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i: integer;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar: Pchar;<br>
		begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar := pchar(str);<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; result := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; while i &lt; length(tmpChar) do begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ord(tmpChar[i]) &lt; $80 then begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 1;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result + 1;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end else begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 2;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result + 3;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; end;<br>
		end;</font><p><font face="Courier New">
		////////////////////////////////////////////////<br>
		//----------取得字符串中的字符个数------------//<br>
		//str:String 源字符串<br>
		//Result:Integer 字符个数,兼容中文双字节<br>
		class function TPduPush.getAnsiLen(Str: string): integer;<br>
		var<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i: integer;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar: Pchar;<br>
		begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar := pchar(str);<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; result := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; while i &lt; length(tmpChar) do begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ord(tmpChar[i]) &lt; $80 then<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 1<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; else<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 2;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result + 1;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; end;<br>
		end;</font></p>
		<p><br>
		<font face="Courier New">
		/////////////////////////////////////////////////<br>
		//---------截取指定长度的utf8字符串------------//<br>
		//str:string 源字符串<br>
		//count:Integer 指定长度&nbsp; 一个汉字占三个字节,长度只能小,不能大<br>
		//Result:string 截取后的utf8字符串<br>
		class function TPduPush.getUTF8String(Str: string; count: Integer): 
		string;<br>
		var<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i, j: integer;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar: Pchar;<br>
		begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp; tmpChar := pchar(str);<br>
		&nbsp;&nbsp;&nbsp;&nbsp; i := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; j := 0;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; result := '';</font></p>
		<p><font face="Courier New">&nbsp;&nbsp;&nbsp;&nbsp; while i &lt; length(tmpChar) do begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if j &gt;= count then break;&nbsp; //英文转码后不能超过指定的位数<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if ord(tmpChar[i]) &lt; $80 then begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result + string(tmpChar[i]);<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 1;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j := j + 1;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end else begin<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if j + 2 &gt;= count then break;&nbsp; //汉字转码后不能超过指定的位数<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; result := result + string(tmpChar[i]) + string(tmpChar[i 
		+ 1]);<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; i := i + 2;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; j := j + 3;<br>
		&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; end;<br>
		&nbsp;&nbsp;&nbsp;&nbsp; end;</font></p>
		<p><font face="Courier New">end;</font></div>
</form>
<form language="javascript" id="Form2" name="Form2" onsubmit="if (!ValidatorOnSubmit()) return false;" method="post" action="http://www.cnblogs.com/liukun966123/archive/2004/09/23/46021.aspx">
</form>

</body>

</html>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -