?
Solved

string to memory stream and back.

Posted on 2003-02-25
5
Medium Priority
?
3,951 Views
Last Modified: 2008-02-26
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
Comment
Question by:moxum
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
5 Comments
 
LVL 27

Expert Comment

by:kretzschmar
ID: 8015551
what about to use a TStringStream?
0
 
LVL 21

Expert Comment

by:ziolko
ID: 8015822
var str: string;

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

ziolko.
0
 
LVL 1

Expert Comment

by:krukmat
ID: 8015992
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
 

Author Comment

by:moxum
ID: 8016028
hmm that doesn't work... (and I also need a way to get the string... not only to store it...
0
 

Accepted Solution

by:
gecqo earned 150 total points
ID: 8019304
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

On Demand Webinar: Networking for the Cloud Era

Ready to improve network connectivity? Watch this webinar to learn how SD-WANs and a one-click instant connect tool can boost provisions, deployment, and management of your cloud connection.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
Do you want to know how to make a graph with Microsoft Access? First, create a query with the data for the chart. Then make a blank form and add a chart control. This video also shows how to change what data is displayed on the graph as well as form…
Suggested Courses
Course of the Month11 days, 11 hours left to enroll

752 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question