Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Decode Base64 into Memo or file
#11
(06-25-2019, 03:06 PM)staff@ergosoft.it Wrote: sorry but I have some problems with the suggestions indicated above ...

Why are you using AnsiString for everything?  You originally tried TNetEncoding, which was first added in Delphi XE7, so you are clearly using a Unicode version of Delphi, so you should be using UnicodeString for everything, not AnsiString at all.  TIdDecoderMIME.DecodeString() and BytesToString() (and TEncoding.GetString()) return a UnicodeString in Delphi 2009 and later.

(06-25-2019, 03:06 PM)staff@ergosoft.it Wrote: if the file to be decoded is "simple" ... to the work ....
if instead there are special characters it does NOT work ...

That can easily happen when you deal in Unicode->ANSI conversions.  Such conversions are lossy.  Stick with Unicode for everything, eg:

Code:
var
  Base64 : String;
  Decoded : String;
  IDBytes : TIdBytes;
begin
  ... 

  //Base64 string in precedent post  (coded64)

  Decoded := TIdDecoderMIME.DecodeString(Base64, IndyTextEncoding_UTF8);

  or:

  IDBytes := TIdDecoderMIME.DecodeBytes(Base64);
  Decoded := BytesToString(IDBytes, IndyTextEncoding_UTF8);

  ... 

  StrToFile('c:\temp\Correct.xml.p7m', Decoded);

  ...

Code:
procedure StrToFile(const FileName, SourceString: String);
var
  Writer : TStreamWriter;
begin
  Writer := TStreamWriter.Create(FileName, False, TEncoding.UTF8);
  try
    Writer.Write(SourceString);
  finally
    Writer.Free;
  end;

  or:

  System.IOUtils.TFile.WriteAllText(FileName, SourceString, TEncoding.UTF8);

end;

(06-25-2019, 04:00 PM)staff@ergosoft.it Wrote: Base64 := BytesToString(IDBytes, IndyTextEncoding_ANSI);
IndyTextEncoding_ANSI is not defined ... error....

There is no IndyTextEncoding_ANSI in Indy.  The equivalent of TEncoding.ANSI is IndyTextEncoding_OSDefault.

That being said:

(05-11-2019, 02:48 PM)staff@ergosoft.it Wrote: I try to decode my string on line here https://www.base64decode.org/ and work... !

this is my string for test coded base64:

MIJAZgYJKo[...snipped...]sgT+0o5ms=

The base64 data you showed originally DOES NOT decode to anything that resembles text.  It is, in fact, binary data instead.  Which makes sense, as you are saving the data to a p7m file, which is an encrypted email format, and encrypted data is binary.  So, you should not be decoding the base64 to a String at all.  Decode it to bytes only, and then save those bytes as-is to your p7m file, eg:

Code:
var
  Base64 : String;
  IDBytes : TIdBytes;
begin
  ...

  //Base64 string in precedent post  (coded64)

  IDBytes := TIdDecoderMIME.DecodeBytes(Base64);

  BytesToFile('c:\temp\Correct.xml.p7m', IDBytes);

  ...


Code:
procedure BytesToFile(const FileName: String; const SourceBytes: array of Byte);
var
  Stream : TFileStream;
begin
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    Stream.WriteBuffer(PByte(SourceBytes)^, Length(SourceBytes));
  finally
    Stream.Free;
  end;
end;

Reply
#12
Hi Rlebeau !!!
OK
Resolved....
your suggestions were precious!

Many thanks
Alessandro Romano
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)