delphi html编码转换,怎样把字符串转化为 Utf8 编码,高手指点

怎样把字符串转化为 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;

可以去搜索一下

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值