• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4309
  • Last Modified:

string to memory stream and back.

Problem:

I have a string that I want to save in a memory stream, send the stream via indy over a socket fetch it in another stream at the other side and retreive the string again.

(I only need the string and stream part, not the socket stuff)
0
moxum
Asked:
moxum
1 Solution
 
kretzschmarCommented:
what about to use a TStringStream?
0
 
ziolkoCommented:
var str: string;

somwhere in read/write method: ...Pointer(str)^...

ziolko.
0
 
krukmatCommented:
try this:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    Memo1: TMemo;
    procedure Button1Click(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function loadStringToStream(text:string):TMemoryStream;
begin
  result:=TMemoryStream.Create;
  result.WriteBuffer(text,length(text));
  result.Seek(0,soFromBeginning);
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  loadStringToStream(edit1.Text);
end;

end.
0
 
moxumAuthor Commented:
hmm that doesn't work... (and I also need a way to get the string... not only to store it...
0
 
gecqoCommented:
Transferring a string to a memory stream can be done by treating the string as an array of char. If you have a  variable string MyString and a TMemoryStream MyStream, you can do the following:

MyStream.Write(MyString[1], Length(MyString));

You have to send your MyString length with the stream to the other side because you have to know its length to get it back from the stream:

SetLength(MyString, MyStringLen);
MyStream.Read(MyString[1], MyStringLen);

0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now