604
社区成员
发帖
与我相关
我的任务
分享
void __fastcall TForm1::Button1Click(TObject *Sender)
{
Memo2->Text = IdEncoderMIME1->Encode( Memo1->Text );
}
void __fastcall TForm1::Button2Click(TObject *Sender)
{
Memo1->Text = IdDecoderMIME1->DecodeToString( Memo2->Text );
}
function ToBytes(const AValue: string; const ALength: Integer; const AIndex: Integer = 1;
const AEncoding: TIdEncoding = en7Bit): TIdBytes; overload;
var
LLength: Integer;
begin
ValidEncoding(AEncoding);
LLength := IndyLength(AValue, ALength, AIndex);
if LLength > 0 then
begin
if AEncoding = enUTF8 then begin
Result := StringToUTF8Bytes(AValue, AIndex, LLength);
end else
begin
// do just a byte to byte copy with no translation. VCL uses ANSI or MBCS.
// With MBCS we still map 1:1
SetLength(Result, LLength);
CopyTIdString(AValue, AIndex, Result, 0, LLength,AEncoding);
end;
end else begin
SetLength(Result, 0);
end;
end;
procedure CopyTIdString(const ASource: String; const ASourceIndex: Integer;
var VDest: TIdBytes; const ADestIndex: Integer; const ALength: Integer = -1;
const AEncoding: TIdEncoding = en7Bit); overload;
{$IFDEF USEINLINE}inline;{$ENDIF}
var
LLength: Integer;
{$IFDEF DOTNET_OR_TEncoding}
i : Integer;
{$ENDIF}
begin
ValidEncoding(AEncoding);
LLength := IndyLength(ASource, ALength, ASourceIndex);
if LLength > 0 then
begin
{$IFDEF DOTNET_OR_TEncoding}
if AEncoding = en8bit then
begin
{
IMPORTANT!!!!
For en8bit, the chars from $80-$9F will wind up being translated to Unicode byte values
and not back to the original $80-$9F values. That might be a good thing if you have a string
that you want to print but that doesn't work too well for when the string is
binary data. The benefit of the Windows 1252 code page is that you always get
charactors you can print.
The routine itself could probably be optimized but I'm not sure how.
}
for i := 0 to LLength-1 do
begin
VDest[ADestIndex+i] := Ord(ASource[ASourceIndex+i]) and $FF;
end;
end
else
begin
GetEncoder(AEncoding).GetBytes(ASource, ASourceIndex{$IFDEF DOTNET}-1{$ENDIF}, LLength, VDest, ADestIndex);
end;
{$ELSE}
// TODO: support UTF8
if AEncoding = enUTF8 then begin
ToDo;
end;
Move(ASource[ASourceIndex], VDest[ADestIndex], LLength);
{$ENDIF}
end;
end;
void __fastcall TForm2::Button1Click(TObject *Sender)
{
TBytes as;
AnsiString str = Memo1->Text;
as.Length = str.Length();
memcpy(&as[0],str.c_str(),as.Length);
Memo2->Text = IdEncoderMIME1->EncodeBytes(as);
}
//---------------------------------------------------------------------------
void __fastcall TForm2::Button2Click(TObject *Sender)
{
TBytes us = IdDecoderMIME1->DecodeBytes( Memo2->Text );
Memo1->Text = AnsiString((char*)&us[0],us.Length);
}
//---------------------------------------------------------------------------
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TMemoryStream *strm;
AnsiString str;
str = "hello大家好";
strm = new TMemoryStream();
try{
strm->Write(str.c_str(),str.Length());
strm->Seek(0,soFromBeginning);
ShowMessage(IdEncoderMIME1->Encode(strm));
}__finally{
strm->Free();
}
}
//IdEncoderMIME1->Encode( src, dest ); 此处缺个参数
IdEncoderMIME1->Encode( src, dest , src->Size);
AnsiString strSrc = Memo1->Text;
AnsiString strDest;
TMemoryStream * src = new TMemoryStream;
src->Write( strSrc.c_str(), strSrc.Length() );
TMemoryStream * dest = new TMemoryStream;
IdEncoderMIME1->Encode( src, dest );
strDest.SetLength( dest->Size );
dest->Read( strDest.c_str(), strDest.Length() );
Memo2->Text = strDest;
TStringStream * src = new TStringStream;
src->WriteString( Memo1->Text );
TStringStream * dest = new TStringStream;
IdEncoderMIME1->Encode( src, dest );
Memo2->Text = dest->DataString;
Encode(Classes::TStream* ASrcStream, Classes::TStream* ADestStream, const int ABytes = 0xffffffff)/* overload */;