【问题标题】:Delphi xe2 encode/decode base 64Delphi xe2 编码/解码 base 64
【发布时间】:2013-06-14 13:07:44
【问题描述】:

有人可以为我提供一个如何使用库Soap.EncdDecd 中的EncodeBase64DecodeBase64 的示例吗?我正在使用Delphi xe2

【问题讨论】:

  • 你自己试过了吗?你读过文档吗?你觉得有什么困难?

标签: delphi delphi-xe2 decode encode base


【解决方案1】:

您没有指定要编码或解码的数据类型。 DecodeBase64EncodeBase64 函数在内部使用 EncodeStreamDecodeStream,理论上您可以使用这些函数基于流对任何类型或数据进行编码或解码(在使用流保存数据之后)。

对于编码/解码字符串,只需直接使用EncodeStringDecodeString 函数即可。

function  EncodeString(const Input: string): string;
function  DecodeString(const Input: string): string;

对于流使用EncodeStreamDecodeStream

procedure EncodeStream(Input, Output: TStream);
procedure DecodeStream(Input, Output: TStream);

EncodeBase64 示例

function  DecodeBase64(const Input: AnsiString): TBytes;
function  EncodeBase64(const Input: Pointer; Size: Integer): AnsiString;

例如,使用EncodeBase64 函数对文件进行编码并返回一个字符串,你可以试试这个(显然你也可以直接使用EncodeStream 函数)。

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

现在要使用DecodeBase64 函数,只需传递一个已编码的字符串,该函数将返回一个 TBytes(字节数组)。

【讨论】:

  • 您好,感谢您的示例,但它不起作用。传递字符串时,出现错误消息:“无法打开特定文件(路径)。系统找不到指定的路径”。我该解决谁?
  • 那是因为文件不存在。如果你想编码一个字符串,只需使用EncodeString 函数。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2022-01-10
  • 1970-01-01
  • 2011-04-03
  • 2011-10-11
  • 2018-11-17
相关资源
最近更新 更多