3

I have a Base64 encoded String that contains PDF data. Using the EncdDecd unit I can decode the String to a Byte Array.

This is where I am having trouble: I tried saving the characters to a String but once it hits a zero value (ASCII = 0 or #0 or $00) the String no longer appends. Example:

uses
  EncdDecd;
var
  EncodedString : String;
  Report : String;
  Base64Bytes: TBytes; // contains the binary data
begin
  Base64Bytes := DecodeBase64(EncodedString);
  for I := 0 to Length(Base64Bytes) - 1 do
    begin
      Report := Report + Chr(Base64Bytes[I]);
    end;

Writing to a text file seems to work better but after renaming it to .PDF the file does not open correctly.

How can I write to a binary file in Delphi? Or even save the data to a stream? Basically I am just trying to take the encoded String and save it to a PDF/binary file, or display the PDF in Delphi.

I have looked around quite a bit and found a possible solution in Saving a Base64 string to disk as a binary using Delphi 2007 but is there another way?

2
  • 2
    Duplicate of Saving a Base64 string to disk as a binary using Delphi 2007. I don't see what new information you're requesting here. If you just want additional options, they belong as answers to the original question, not this duplicate. Commented Oct 31, 2011 at 16:41
  • what have you tried? what was the problem? How do you "append" the string? please post your code using "EncdDecd namespace". Also, how do you change a text file to a pdf? again post your code. There are many ways in Delphi to transform a string and save it in binary. The link you posted is quite a compact code to do it. Commented Oct 31, 2011 at 16:45

1 Answer 1

9

This should do it:

procedure DecodeBaseToFile(const FileName: string; 
  const EncodedString: AnsiString);
var
  bytes: TBytes;
  Stream: TFileStream;
begin
  bytes := DecodeBase64(EncodedString);
  Stream := TFileStream.Create(FileName, fmCreate);
  try
    if bytes<>nil then
      Stream.WriteBuffer(bytes[0], Length(bytes));
  finally
    Stream.Free;
  end;
end;

Note: I have only compiled this in my head.

Sign up to request clarification or add additional context in comments.

5 Comments

@Trevor: you can alternatively use a TByteStream instead. It is a TMemoryStream descendant, so it has a SaveToFile() method available. That way, you don't have to instantiate TFileStream manually.
@Trevor: upvoted it for you, but you'll have to accept it as the answer yourself (check mark underneath the up/down vote arrows).
I created a empty unit, in uses need uses System.Classes, System.SysUtils, synacode, IdCoder, IdCoderMIME; and I need change the line Decodebase64 to bytes := TIdDecoderMIME.DecodeBytes(EncodedString);
@MárcioRossato That's using a different base64 encoder
yes, I need change because I have not found the correct uses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.