Board index » delphi » How to convert strings to nul-terminated strings?

How to convert strings to nul-terminated strings?

Please HELP!
I don't understand why my routine to convert strings to pascal-typed
strings doesn't work! Look at my other message which I posted and is called
'weird things happen!' I know there's the StrPCopy but that doesn't seem to
work in all cases.

Thanx in advance!

 

Re:How to convert strings to nul-terminated strings?


Quote
Edwin Litan wrote:

> Please HELP!
> I don't understand why my routine to convert strings to pascal-typed
> strings doesn't work! Look at my other message which I posted and is called
> 'weird things happen!' I know there's the StrPCopy but that doesn't seem to
> work in all cases.

There 2 solutions:
        a/- one to obtain only a PChar pointer address to pass to another
function,
        b/- one to copy in a buffer

a/- Convert to a PChar pointer:

        Var St : String;
            PtrSt : PChar;              
        Begin
          St := 'What you want';
          St[1+Length(St)] := #0;  { Add #00 to the end without changing length

Quote
}

          PtrSt := Addr (St[1]);   { Now PtrSt point to a PChar string }
        End;

b/- Copy in a buffer:
        Var St : String;
            Buff : Array [0..256] Of Char;
        Begin
          St := 'What you want';
          St[1+Length(St)] := #0;  { Add #00 to the end without changing length

Quote
}

          StrCopy (Buff, @St[1]);  { Buff contains a cpy of your string }
        End;

Regards,
        Philippe.

Re:How to convert strings to nul-terminated strings?


Quote
Edwin Litan wrote:

> Please HELP!
> I don't understand why my routine to convert strings to pascal-typed
> strings doesn't work! Look at my other message which I posted and is called
> 'weird things happen!' I know there's the StrPCopy but that doesn't seem to
> work in all cases.

There 2 solutions:
        a/- one to obtain only a PChar pointer address to pass to another
function,
        b/- one to copy in a buffer

a/- Convert to a PChar pointer:

        Var St : String;
            PtrSt : PChar;              
        Begin
          St := 'What you want';
          St[1+Length(St)] := #0;  { Add #00 to the end without changing length

Quote
}

          PtrSt := Addr (St[1]);   { Now PtrSt point to a PChar string }
        End;

b/- Convert to a PChar pointer:
        Var St : String;
            Buff : Array [0..256] Of Char;
            PtrSt : PChar;              
        Begin
          St := 'What you want';
          St[1+Length(St)] := #0;  { Add #00 to the end without changing length

Quote
}

          StrCopy (Buff, @St[1]);  { Buff contains a cpy of your string }
        End;

Regards,
        Philippe.

Re:How to convert strings to nul-terminated strings?


Quote
"Edwin Litan" <Li...@ctr.minbuza.nl> wrote:
>Please HELP!
>I don't understand why my routine to convert strings to pascal-typed
>strings doesn't work! Look at my other message which I posted and is called
>'weird things happen!' I know there's the StrPCopy but that doesn't seem to
>work in all cases.

In the past 90 days I have not retrieved an article with such a
subject, nor in the same period have I retrieved an article with
your name in the "from" field that discussed strings in any
manner.  I'm not certain how others feel, but I'm not willing to
search dejanews to see what you've posted.

StrPCopy converts a Pascal string to a nul-termianted string.  Its
the programmer's responsibility to insure that the target array is
large enough to hold the Pascal string.  Use StrLCopy to restrict
the number of characters copied from the Pascal string.

StrPas converts a nul-terminated string to a Pascal string.  If
the length of the nul-terminated string is longer than the target
Pascal string, then the result is truncated to the maximum length
allowed by the size of the Pascal string.

    ...red

Re:How to convert strings to nul-terminated strings?


For a followup on this discussion,... it doesn't seem to help! I still get
garbage or nothing at all. Please execute the program (BP windows 7.0) and
you'll see what
I mean....

Regard,

Edwin.

begin 600 test.pas
M56YI="!497-T.PT*#0I);G1E<F9A8V4-"@T*5F%R#0H@($1B365S<V%G92 @
M(#H@4W1R:6YG.PT*#0H@(%!R;V-E9'5R92!'9717365S<V%G93L-"@T*#0I)
M35!,14U%3E1!5$E/3@T*#0H@(%!R;V-E9'5R92!'9717365S<V%G93L-"B @
M0F5G:6X-"B @("!$8DUE<W-A9V4Z/2=4:&ES(&ES('=E:7)D(2<[#0H@($5N
*9#L-"F5N9"X-"B!$
`
end
end

begin 600 weird.pas
M4')O9W)A;2!796ER9#L-"@T*>R12+7U[)$XK?7LD12M]>R18*WT-"@T*#0I5
M<V5S(%1E<W0L5VEN5'EP97,L5VEN4')O8W,L4W1R:6YG<RQ7:6Y#<G0[#0H-
M"D9U;F-T:6]N(%1O4%,H3&EN92 Z(%-T<FEN9RDZ4$-H87([#0I"96=I;@T*
M("!,:6YE.CU,:6YE*R,P.PT*("!4;U!S.CU 3&EN95LQ73L-"D5N9#L-"@T*
M#0H-"D)%1TE.#0H@($=E=%=-97-S86=E.PT*("!-97-S86=E0F]X*$=E=$9O
M8W5S+"!4;U!S*$1B365S<V%G92DL("=796ER9" A)RP@;6)?3VL@3W(@;6)?
M26-O;D5X8VQA;6%T:6]N*3L-"B @5W)I=&5,;BA$8DUE<W-A9V4I.PT*14Y$
#+@T*
`
end
end

Re:How to convert strings to nul-terminated strings?


Quote
"Edwin Litan" <Li...@ctr.minbuza.nl> wrote:
>For a followup on this discussion,... it doesn't seem to help! I still get
>garbage or nothing at all. Please execute the program (BP windows 7.0) and
>you'll see what
>I mean....
>Regard,
>Edwin.

Try changing your code like so:

Function ToPS( var Line : String):PChar;
(*     added   ^^^        *)
Begin
  Line:=Line+#0;
  ToPs:=@Line[1];
End;

I should point out that the cannonical way of doing this is to use
StrPCopy(), though you should be safe in most cases with what you
have, do be careful that Line has enough room in it for the null
character to be added.

Regards,

Stephen Posey
slpo...@concentric.net

Re:How to convert strings to nul-terminated strings?


Thanx Stephen! It solved my problem... (I had some trouble spotting the
difference between [ToPs(var Line : String):PChar] and [ToPs(Line :
String):PChar] but it sertainly fixes the bug!

Thanx again!!!

Guess this closes the case.

Re:How to convert strings to nul-terminated strings?


Quote
"Edwin Litan" <Li...@ctr.minbuza.nl> wrote:
>For a followup on this discussion,... it doesn't seem to help! I still get
>garbage or nothing at all. Please execute the program (BP windows 7.0) and
>you'll see what
>I mean....

No need to execute anything.  The error leaps off the page.

Quote
>Function ToPS(Line : String):PChar;
>Begin
>  Line:=Line+#0;
>  ToPs:=@Line[1];
>End;

In the above, line exists on the stack only while the function
ToPS is active.  When ToPS terminates, the stack pointer is
adjusted such that line is released.  The error is that you return
a pointer to this soon to be released region of the stack,  The
garbage you experience is simply the variables of other procedures
and/or interrupt service routines that become active after the
pointer has been returned.

Any solution requires that the scope of the text pointed to by the
pChar result will remain valid. You could have ToPS allocate heap
space using StrNew, but you would then need to insure that the
text was eventually released w/ StrDispose.

You can define line as a VAR parameter and use the original
variable space to hold you nul-terminated string.  I think this
may be closer to what you were trying to do.

Function ToPS(VAR Line : String):PChar;
Begin
  {$IFOPT R+}
    If Length(Line) > Pred(High(Line)) Then RunError(201);
  {$ENDIF}
  { Terminate line w/o altering value of Pascal string }
  Line[Succ(Length(Line))] := #0;
  ToPs := @Line[1];
End;

If you prefer to have ToPS defined to use open parameters, simply
use the {$P+} directive.  If you had been using an older version
of TP that doesn't have the High function, then you would have to
change "> Pred(High(Line))" to "> Pred(Sizeof(Line))" or hardcode
it using something like "> 254".

    ...red

Re:How to convert strings to nul-terminated strings?


Hi Philippe,

You might want to try this...

Personally, I think that keeping the two variables is a waste a space and
the
conversion, a waste of time.  It is not intuitive, though; and it might be
a little
more difficult to maintain.

program LittleTrick;
var
        sMyPascalString : string;
        sMyCString : array[0..254] of byte;

begin
        // This is the assignment to a Pascal string
        sMyString := 'Hello';
        // Convert to null-terminated
        sMyString := sMyString + Chr(0)
        // Now pass to functions expecting a PChar as the
        // "pointer to the first character in the Pascal string"
        // It works because you added the NULL character to the end
        StrCopy(sMyCString, @sMyString[1]);
        // Convert back to Pascal
        Copy(sMyString, 1, Length(sMyString));
end;

Philippe <p.tec...@idls.izarbel.tm.fr> wrote in article
<3377842E.6...@idls.izarbel.tm.fr>...

Quote
> Edwin Litan wrote:

> > Please HELP!
> > I don't understand why my routine to convert strings to pascal-typed
> > strings doesn't work! Look at my other message which I posted and is
called
> > 'weird things happen!' I know there's the StrPCopy but that doesn't
seem to
> > work in all cases.

> There 2 solutions:
>    a/- one to obtain only a PChar pointer address to pass to another
> function,
>    b/- one to copy in a buffer

> a/- Convert to a PChar pointer:

>    Var St : String;
>        PtrSt : PChar;              
>    Begin
>      St := 'What you want';
>      St[1+Length(St)] := #0;  { Add #00 to the end without changing length
> }
>      PtrSt := Addr (St[1]);   { Now PtrSt point to a PChar string }
>    End;

> b/- Copy in a buffer:
>    Var St : String;
>        Buff : Array [0..256] Of Char;
>    Begin
>      St := 'What you want';
>      St[1+Length(St)] := #0;  { Add #00 to the end without changing length
> }
>      StrCopy (Buff, @St[1]);  { Buff contains a cpy of your string }
>    End;

> Regards,
>    Philippe.

Re:How to convert strings to nul-terminated strings?


Quote
Philippe (p.tec...@idls.izarbel.tm.fr) writes:
> Edwin Litan wrote:

>> Please HELP!
>> I don't understand why my routine to convert strings to pascal-typed
>> strings doesn't work! Look at my other message which I posted and is called
>> 'weird things happen!' I know there's the StrPCopy but that doesn't seem to
>> work in all cases.

> There 2 solutions:
>    a/- one to obtain only a PChar pointer address to pass to another
> function,
>    b/- one to copy in a buffer

This is an odd idea, but since PChar is a pointer, couldn't you just add #0
 to the end of your pascal string, and set the PChar pointer to
 string[1]?

You could still manipulate the string as a pascal string, and as long as
 you always add #0 the end the Pchar pointer will work?

I've never done that but it seems possible.  Am I missing anything?

Steve
--
   * * *   Author of Imagic and APE - The Atari Peripheral Emulator!   * * *
   * * *       Turn your 8-bit Atari into a powerhouse with APE!       * * *
  *  *  *         Ape Homepage: http://www.nacs.net/~classics         *  *  *
 !! Request my *FOR SALE* LISTING OF CLASSIC VIDEO GAME STUFF !! 2000+ Lines !!

Re:How to convert strings to nul-terminated strings?


On 11 May 1997 09:51:33 GMT, "Edwin Litan" <Li...@ctr.minbuza.nl>
wrote:

Quote
>Please HELP!
>I don't understand why my routine to convert strings to pascal-typed
>strings doesn't work! Look at my other message which I posted and is called
>'weird things happen!' I know there's the StrPCopy but that doesn't seem to
>work in all cases.

>Thanx in advance!

Hi Edwin

To convert from a Pascal String to a null-terminated string:- Suppose
you have:-

var S: String; Pch: PChar;

begin
 S:= 'This is my string';
{add a null}
S:= S + #0;
PCh:= @S[1]; { PCh is now the null string equivalent}
......

end;

Michael Glover -  glover<at>globalnet.co.uk
(Surrey, UK http://www.users.globalnet.co.uk/~glover/ )
{I've disguised my email address from non-human spam bots.}

Re:How to convert strings to nul-terminated strings?


In article <33763877.15325...@news.globalnet.co.uk> of Mon, 16 Jun 1997
19:50:00 in comp.lang.pascal.borland, M G <g...@globalnet.co.uk> wrote:

Quote
>To convert from a Pascal String to a null-terminated string:- Suppose
>you have:-

>var S: String; Pch: PChar;

>begin
> S:= 'This is my string';
>{add a null}
>S:= S + #0;
>PCh:= @S[1]; { PCh is now the null string equivalent}
>......

>end;

Fails if the original string was of maximum length and not immediately
followed by #0.  See other responses - AFAIR the answer is to use
StrPas.  Consider whether you can convert the string, or need a new
null-terminated version.

It may be quicker to use
        if Length(S)=255 then FAIL else S[Succ(Length(S))]:=#0 ;

--
John Stockton, Surrey, UK.    j...@merlyn.demon.co.uk    Turnpike v1.12    MIME.
  Web URL: http://www.merlyn.demon.co.uk/ -- includes FAQqish topics and links.
  Correct 4-line sig separator is as above, a line comprising "-- " (SoRFC1036)
  Before a reply, quote with ">" / "> ", known to good news readers (SoRFC1036)

Other Threads