Delphi-UTF8转GBK

 

/////////////////////////////////////////////
function TXml.getValue(str: string): string;//传入的UTF8字符串
var
s: string;
begin
if FBase64 then
begin
s := TNetEncoding.Base64.EncodeBytesToString(TEncoding.ANSI.GetBytes(str));
s := s.Replace(#13#10,'');
Result := s;//输出UTF8转GBK后的base64字符串
end
else
Result := str;
end;
///////////////////////////////////////////////////////
function AnsiBase64Encode(s : string) : string; //传入的UTF8字符串
var base64 : TIdEncoderMIME;
tmpBytes : TBytes;
begin
base64 := TIdEncoderMIME.Create(nil);
try
base64.FillChar := '=';
tmpBytes := TEncoding.ANSI.GetBytes(s);
Result := base64.EncodeBytes(TIdBytes(tmpBytes)); //Delphi默认加密会78个字符进行换行,后者过滤掉换行//Result := base64.EncodeBytes(TIdBytes(tmpBytes)).Replace(sLineBreak, '', [rfReplaceAll]);
finally
base64.Free;
end;
end;


function TXml.getValue(str: string): string;//传入的UTF8字符串
var
s: string;
begin
if FBase64 then
begin
s := AnsiBase64Encode(str);
s := s.Replace(#13#10,'');
Result := s;//输出UTF8转GBK后的base64字符串
end
else
Result := str;
end;

 

posted on 2023-04-11 23:52  疯狂delphi  阅读(367)  评论(0编辑  收藏  举报

导航