Base64编码字符串到二进制文件
我有一个包含 PDF 数据的 Base64 编码字符串。使用 EncdDecd 单元我可以将字符串解码为字节数组。
这就是我遇到麻烦的地方:我尝试将字符保存到字符串中,但是一旦它达到零值(ASCII = 0
或 #0
或 $00< /code>) 字符串不再追加。示例:
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;
写入文本文件似乎效果更好,但将其重命名为 .PDF
后,文件无法正确打开。
如何在 Delphi 中写入二进制文件?或者甚至将数据保存到流中?基本上我只是想获取编码的字符串并将其保存到 PDF/二进制文件,或在 Delphi 中显示 PDF。
我环顾四周并找到了一个可能的解决方案 使用 Delphi 2007 将 Base64 字符串作为二进制文件保存到磁盘 但还有其他方法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这应该可以做到:
注意:我只是在脑海中编译了这个。
This should do it:
Note: I have only compiled this in my head.