欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

delphi TByteArray 装换成base64

程序员文章站 2022-06-07 21:21:26
...
unit EncodingUtil;

interface

uses
  SysUtils, Classes, Types, EncdDecd;

function BytesToBase64(const bytes: TByteArray): string;
function StreamToBase64(AStream: TStream): string;

implementation

function BytesToBase64(const bytes: TByteArray): string;
var
  memoryStream: TMemoryStream;
begin
  memoryStream := TMemoryStream.Create;
  memoryStream.WriteBuffer(bytes[0], Length(bytes));
  memoryStream.Seek(0, soFromBeginning);
  Result := StreamToBase64(memoryStream);
  memoryStream.Free;
end;

function StreamToBase64(AStream: TStream): string;
var
  objSS: TStringStream;
begin
  objSS := TStringStream.Create('');
  try
    EncodeStream(AStream, objSS); // Delphi7 自带unit EncdDecd的方法
    Result := objSS.DataString;
  finally
    FreeAndNil(objSS);
  end;
end;
end.
相关标签: delphi