Delphi 中将TMemoryStream转换为'String'


本文整理自网络,侵删。

 
function StreamToString(aStream: TStream): string;
var
  SS: TStringStream;
begin
  if aStream <> nil then
  begin
    SS := TStringStream.Create('');
    try
      SS.CopyFrom(aStream, 0);  // No need to position at 0 nor provide size
      Result := SS.DataString;
    finally
      SS.Free;
    end;
  end else
  begin
    Result := '';
  end;
end;



function StreamToString(const Stream: TStream; const Encoding: TEncoding): string;
var
  StringBytes: TBytes;
begin
  Stream.Position := 0;
  SetLength(StringBytes, Stream.Size);
  Stream.ReadBuffer(StringBytes, Stream.Size);
  Result := Encoding.GetString(StringBytes);
end;
它仅在Delphi XE7中经过测试。

相关阅读 >>

Delphi向imagelist中加入png类型的资源图片

Delphi 输入法设置(imemode与imename)

Delphi 在消息窗口中显示进度条

Delphi 文件转换base64

Delphi 获取文件所在路径

Delphi winapi: getparent - 获取指定窗口的父窗口句柄

Delphi使用spcomm串口通信 串口号大于10出错的解决办法

Delphi winapi: getwindow - 获取与指定窗口具有指定关系的窗口的句柄

Delphi winapi: getwindowthreadprocessid - 获取指定窗口的进程 id 或线程 id

Delphi 类和对象

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...