System.SysUtils

一、TStringStream方法

Strem>String

TMemoryStream to String

stm: TStream;

ss: TStringStream;

#include "System.SysUtils.hpp"

DataString

image.picture.savetoStream(stm);

ss := TStringStream.Create('', TEncoding.Unicode);

ss.CopyFrom(stm, 0);
str := ss.DataString;

stm.Free;
ss.Free;

String>Strem

stm: TStream;
ss: TStringStream;

stm := TMemoryStream.Create;
ss := TStringStream.Create('', TEncoding.Unicode);

ss.WriteString(str);

ss.Position := 0;

stm.CopyFrom(ss, ss.Size);

stm.Position := 0;

二、TBytes和stringof转换

Strem>String

abyte: TBytes;

SetLength(abyte, stm.Size);
stm.Read(abyte, stm.Size);
str := stringof(abyte);

String>Strem

abyte := BytesOf(gstr);
stm.Write(abyte, Length(abyte));

stm.Position := 0;
img.picture.LoadFromStream(stm);

function BytesOf(const Val: UnicodeString): TBytes;
begin
Result := TEncoding.Default.GetBytes(Val);
end;

function StringOf(const Bytes: TBytes): UnicodeString;
begin
if Assigned(Bytes) then
Result := TEncoding.Default.GetString(Bytes, Low(Bytes), High(Bytes) + 1)
else
Result := '';
end;

05-11 22:26