delphi2007 教程

delphi2007 教程

首页 新随笔 联系 聚合 管理
  1013 Posts :: 0 Stories :: 28 Comments :: 0 Trackbacks
怎样把字符串转化为 Utf8 编码,高手指点 VCL组件开发及应用
http://www.delphi2007.net/DelphiVCL/html/delphi_20061222153725182.html
请问indy是不是有控件可以转,  
  我怎么样测试是不是转化正确了?  
   
  高手不奢赐教!!!!!!!谢谢

sadfsafa

//Utf8存、取  
  procedure   TForm1.Button1Click(Sender:   TObject);  
  var  
      S:   string;  
  begin  
      //存  
      with   TMemoryStream.Create   do   try  
          S   :=   #$EF#$BB#$BF;  
          Write(S[1],   Length(S));  
          S   :=   AnsiToUtf8(Memo1.Text);  
          Write(S[1],   Length(S));  
          Position   :=   0;  
          SaveToFile('c:\temp\temp.txt');  
      finally  
          Free;  
      end;  
  end;  
   
  procedure   TForm1.Button2Click(Sender:   TObject);  
  var  
      S:   string;  
  begin  
      //取  
      if   not   FileExists('c:\temp\temp.txt')   then   Exit;  
      with   TMemoryStream.Create   do   try  
          LoadFromFile('c:\temp\temp.txt');  
          SetLength(S,   Size);  
          Read(S[1],   Length(S));  
          if   Copy(S,   1,   3)   <>   #$EF#$BB#$BF   then   Exit;  
          Memo2.Text   :=   Utf8ToAnsi(Copy(S,   4,   MaxInt));  
      finally  
          Free;  
      end;  
  end;  
   
 

function   EncodeUTF8(const   s:WideString;   maxlen:   integer):   String;  
  var  
      i,len:Integer;  
      cur:Integer;  
      t:   String;  
      cv:   Byte;  
  //     dv:   TDateTime;//   限时试用  
  begin  
  //     dv   :=   Date();//   限时试用  
      Result:='';  
      len:=Length(s);  
      i:=1;  
      while   i   <=   len   do  
      begin  
          cur   :=   ord(s[i]);   //BCD转换  
          if   cur   <=   $7F   then   //单字节  
          begin  
              t   :=   Char(cur);  
  //             Result   :=   Result   +   t;  
          end  
          else   if   cur   <=   $7FF   then   //双字节  
          begin  
              t   :=   Char($80   +   cur   and   $3F);     cur   :=   cur   shr   6;  
              t   :=   Char($C0   +   cur)+   t;  
  //             Result   :=   Result   +   t;  
          end  
          else   if   cur   <=   $FFFF   then   //三字节  
          begin  
              t   :=   Char($80   +   cur   and   $3F);     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($E0   +   cur)   +   t;  
  //             Result   :=   Result   +   t;  
          end  
          else   if   cur   <=   $1FFFFF   then   //四字节  
          begin  
              t   :=   Char($80   +   cur   and   $3F);     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($F0   +   cur)+   t;  
  //             Result   :=   Result   +   t;  
          end  
          else   if   cur   <=   $3FFFFFF   then   //五字节  
          begin  
              t   :=   Char($80   +   cur   and   $3F);     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($F8   +   cur)+   t;  
  //             Result   :=   Result   +   t;  
          end  
          else   //if   cur   <=   $7FFFFFFF   then   //六字节  
          begin  
              t   :=   Char($80   +   cur   and   $3F);     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($80   +   cur   and   $3F)   +   t;     cur   :=   cur   shr   6;  
              t   :=   Char($FC   +   cur)+   t;  
  //             Result   :=   Result   +   t;  
          end;  
          if   Length(Result)   +   Length(t)   >   maxlen   then  
              Break;  
          Result   :=   Result   +   t;  
          inc(i);  
  //   限时试用begin  
  //         if   dv   >   38564   +   30   then  
  //             Exit;  
  //   限时试用end  
      end;  
  end;  
   
  function   DecodeUTF8(const   s:string):WideString;  
  var  
      wv:   integer;  
      i,len:   integer;  
      cv:   Byte;  
  begin  
      Result   :=   '';  
      len   :=   Length(s);  
      i   :=   1;  
      while   i   <=   len   do  
      begin  
          cv   :=   Byte(s[i]);  
          if   cv   <=   $7F   then   //   单字节  
              wv   :=   cv  
          else   if   cv   <   $C0   then   //   Error  
   
          else   if   cv   <   $E0   then   //   双字节  
          begin  
              wv   :=   cv   and   $1F;   wv   :=   wv   shl   6;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   +   cv   and   $3F;  
          end  
          else   if   cv   <   $F0   then   //   三字节  
          begin  
              wv   :=   cv   and   $1F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
          end  
          else   if   cv   <   $F8   then   //   四字节  
          begin  
              wv   :=   cv   and   $1F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
          end  
          else   if   cv   <   $FC   then   //   五字节  
          begin  
              wv   :=   cv   and   $1F;   wv   :=   wv   shl   6;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
          end  
          else   if   cv   <   $FE   then   //   六字节  
          begin  
              wv   :=   cv   and   $1F;   wv   :=   wv   shl   6;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
              Inc(i);   cv   :=   Byte(s[i]);   wv   :=   wv   shl   6;   wv   :=   wv   +   cv   and   $3F;  
          end  
          else   //   Error  
              ;  
          Inc(i);  
          Result   :=   Result   +   WideChar(wv);  
      end;  
  end;  
   
 

 
      可以去搜索一下

posted on 2008-11-18 14:27 delphi2007 阅读(390) 评论(0)  编辑 收藏 引用
只有注册用户登录后才能发表评论。