Base64 to Binary (Delphi)

烈酒焚心 提交于 2020-03-13 06:42:10

问题


I used Binary to Base64 function that you answered : Binary to Base64 (Delphi)

I successfully encode a file to base64 string and write it to MsSQL2008 database, but i want to ask a question: How can i write this file to disk again with using EncdDecd.pas?


回答1:


As always, David answered sufficiently. Although I can't resist to give a slightly different solution using some of the goodies from the recent Delphi versions.

procedure DecodeFile(const base64: AnsiString; const FileName: string);
var
  stream: TBytesStream;
begin
  stream := TBytesStream.Create(DecodeBase64(base64));
  try
    stream.SaveToFile(Filename);
  finally
    stream.Free;
  end;
end;



回答2:


This function will take a base64 encoded string, decode it, and write the resulting byte array to a file.

procedure DecodeToFile(const base64: AnsiString; const FileName: string);
var
  stream: TFileStream;
  bytes: TBytes;
begin
  bytes := DecodeBase64(base64);
  stream := TFileStream.Create(FileName, fmCreate);
  try
    if bytes<>nil then
      stream.Write(bytes[0], Length(Bytes));
  finally
    stream.Free;
  end;
end;

To explain what is happening here, the first line

bytes := DecodeBase64(base64);

performs the decode and returns the decoded binary contents of the file in a TBytes variable. TBytes is simply an array of bytes.

The next step is to create the file. The idiomatic way to write files in Delphi is to use streams. In this case we want a TFileStream.

stream := TFileStream.Create(FileName, fmCreate);

The fmCreate option means that if the file already exists, it will be replaced and overwritten by what we write.

The final step is to write the contents of the byte array to the file

if bytes<>nil then
  stream.Write(bytes[0], Length(Bytes));

The if bytes<>nil check is to handle the case where the base64 string decodes to an empty array. If we were to remove that check then the following line would result in a runtime error if you were running with range checking enabled (which you should be doing). The call to stream.Write should be self-explanatory.




回答3:


I have a very old Delphi2006(v10.0.2558.35231 Update 2) and had to decode base64 UTF8 encoded input strings. I finally figured it out and heres an example for anyone interested.

  Uses
    IdCoderMIME; // Indy9
  var
    decoder: TIdDecoderMIME;
    str: WideString;
  - - - 

  decoder := TIdDecoderMIME.Create(nil);
  str := base64DecodeUTF8(decoder, b64sourcestr);
  decoder.Free;
  - - - 

  function base64DecodeUTF8(decoder:TIdDecoderMIME; str:String): WideString;
  var
    stream:TMemoryStream;
    utf8: UTF8String;
    //idx:Integer;
  begin
    stream := TMemoryStream.Create;
    try
      decoder.DecodeToStream(str, stream);
      setString(utf8, PChar(stream.Memory), stream.Size);
      Result := UTF8Decode(utf8);
      //for idx := 0 to stream.Size-1 do begin
      //  Writeln(PChar(stream.Memory)[idx] + ' ' + IntToStr(ORD(PChar(stream.Memory)      [idx])) );
      //end;
    finally
      stream.Free;
    end;
  end;



回答4:


uses
  Soap.EncdDecd;

function TForm1.EncodeFile(const FileName: string): AnsiString;
var
  MemStream: TMemoryStream;
begin
  MemStream := TMemoryStream.Create;
  try
    MemStream.LoadFromFile(Filename);
    Result := EncodeBase64(MemStream.Memory, MemStream.Size);
  finally
    MemStream.Free;
  end;
end;

function TForm1.DecodeFile(const base64: AnsiString): TBytesStream;
begin
 Result := TBytesStream.Create(DecodeBase64(base64));
end;



回答5:


After looking into Soap.EncdDecd the one can find more platform independent way, as it's DecodeBase64 uses universal (no AnsiString) methods from System.NetEncoding.

Based on Uwe's sample:

uses
  ...
  System.Classes,
  System.NetEncoding;

...
procedure DecodeFile(const base64: String; const FileName: string);
var
  stream: TBytesStream;
begin
  stream := TBytesStream.Create(TNetEncoding.Base64.DecodeStringToBytes(base64));
  try
    stream.SaveToFile(Filename);
  finally
    stream.Free;
  end;
end;


来源:https://stackoverflow.com/questions/8768652/base64-to-binary-delphi

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!