怎样把字符串转化为 Utf8 编码,高手指点
发布日期:2021-11-10 09:01:20 浏览次数:1 分类:技术文章

本文共 4085 字,大约阅读时间需要 13 分钟。

请问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;  
   
 

 

      可以去搜索一下

转载于:https://www.cnblogs.com/delphi2007/archive/2008/11/18/1335918.html

转载地址:https://blog.csdn.net/aibi9196/article/details/101304696 如侵犯您的版权,请留言回复原文章的地址,我们会给您删除此文章,给您带来不便请您谅解!

上一篇:【在线等,马上给分】修改树节点后刷新树,怎么定位刚才修改的节点?
下一篇:用WNetAddConnection2建立映射后,如何隐藏映射盘符?

发表评论

最新留言

表示我来过!
[***.240.166.169]2024年03月25日 02时15分23秒