Board index » delphi » Simple String to char question

Simple String to char question

I got a string from a Memo and i want to assign this text to a array called
buf

...
  var
    buf: array[1..$FFFF] of char;
  begin
    repeat
      buf[pos] := copy(inserttext.Text,pos,1);        // copies byte on
position 'pos' length 1 to
        { this one does not work! }
      pos := pos + 1;
    until pos >= length(inserttext.Text);                // until the end
position is reached
  end;
...

how can i solve that simple problem?
already got it once to work but can't remembera nd could not find it in the
help file

thanks

Ludwig Moser

 

Re:Simple String to char question


have you tried saving the memo with savetofile then using blockread to read it
back into your buffer.

Re:Simple String to char question


Quote
"Ludwig Moser" <csad2...@uibk.ac.at> wrote in message

news:gTjz7.133363$Pg3.3945265@news.chello.at...

Quote
> I got a string from a Memo and i want to assign this text to a array
called
> buf

> ...
>   var
>     buf: array[1..$FFFF] of char;

    s : string;

s := InsertText.Text;
Move (@s [1], buf, Min (Length (s), SizeOf (buf));

or
    s : string; i : integer;

s := InsertText.Text;
for i := 1 to (Min (Length (s), SizeOf (buf))) do
    buf [i] := s [i];

Re:Simple String to char question


In article <gTjz7.133363$Pg3.3945...@news.chello.at>, "Ludwig Moser"

Quote
<csad2...@uibk.ac.at> writes:
>I got a string from a Memo and i want to assign this text to a array called
>buf

One of the simplest is most likely CopyMemory ...

  CopyMemory(@Buf, @Memo1.Lines[1][1], Length(Memo1.Lines[1]));

But it depends on what you are going to do with it then. It might be overall
easier to put it in a string and treat the string as an array starting at [1].

Alan Lloyd
alangll...@aol.com

Re:Simple String to char question


got it by myself very simple way!

repeat
  addbuf[pos] := inserttext.Text[pos];
  inc(pos);
until pos >= length(inserttext.Text);

"Bruce Roberts" <b...@bounceitattcanada.xnet> schrieb im Newsbeitrag
news:FVkz7.69784$TW.367170@tor-nn1.netcom.ca...

Quote

> "Ludwig Moser" <csad2...@uibk.ac.at> wrote in message
> news:gTjz7.133363$Pg3.3945265@news.chello.at...
> > I got a string from a Memo and i want to assign this text to a array
> called
> > buf

> > ...
> >   var
> >     buf: array[1..$FFFF] of char;

>     s : string;

> s := InsertText.Text;
> Move (@s [1], buf, Min (Length (s), SizeOf (buf));

> or
>     s : string; i : integer;

> s := InsertText.Text;
> for i := 1 to (Min (Length (s), SizeOf (buf))) do
>     buf [i] := s [i];

Re:Simple String to char question


Quote
"Ludwig Moser" <csad2...@uibk.ac.at> wrote in message

news:j_lz7.135540$Pg3.3995634@news.chello.at...

Quote
> got it by myself very simple way!

> repeat
>   addbuf[pos] := inserttext.Text[pos];
>   inc(pos);
> until pos >= length(inserttext.Text);

Just remember that the above will fail if InsertText.Text = ''. You might
want to consider

while pos <= Length (InsertText.Text) do
    begin
    . . .
    end;

I'm not quite sure how smart the Delphi optimizer is, but I suspect that
'Length (InsertText.Text)' is going to be re-evaluated on every iteration of
the repeat or while loops. This will make either form less efficient than

    for i := 1 to Length (InsertText.Text) do
        addBuf [i] := InsertText.Text [i];

since the terminating condition of the for loop is evaluated once before the
loop is entered.

Since InsertText.Text is a property in all of the above cases it is probably
more efficient to use a temporary string variable in the loop. This is so
because the property may use a read accessor (i.e. a function method) to
return the value, as opposed to directly accessing memory.

Other Threads