Board index » delphi » convert string to pchar

convert string to pchar

hi,

how do i convert a string to a pchar?

i allready know how to convert a pchartostr:

Function PCharToStr( p: pChar; Len: WORD ): String;
Var
   s: String;
Begin
  SetLength( s, Len );
  Move( p^, s[1], Len );
  If POS( #0, s ) > 0 Then
     SetLength( s, POS( #0, s ) - 1 );
  RESULT := s;
End

please help,

Vincent

 

Re:convert string to pchar


Use the StrPCopy function to copy from a Pascal-style string to
null-terminated string.

Quote
Vincent Vijt <vi...@planetinternet.be> wrote in message

news:7ufrer$er2$3@news.planetinternet.be...
Quote
> hi,

> how do i convert a string to a pchar?

> i allready know how to convert a pchartostr:

> Function PCharToStr( p: pChar; Len: WORD ): String;
> Var
>    s: String;
> Begin
>   SetLength( s, Len );
>   Move( p^, s[1], Len );
>   If POS( #0, s ) > 0 Then
>      SetLength( s, POS( #0, s ) - 1 );
>   RESULT := s;
> End

> please help,

> Vincent

Re:convert string to pchar


You may try this:

var
  PCh: PChar;
  S   : string;
begin
  PCh:=StrNew('This is the string');
//or
  S:='This is the string';
  PCh:=StrNew(S);
[...]
  StrDispose(PCh);

...however, a string may be typecasted to a PChar whenever you want:

PCh:=PCh(S);
PCh:=S;

--

Bjoerge

Vincent Vijt skrev i meldingen
<7ufrer$er...@news.planetinternet.be>...

Quote
>hi,

>how do i convert a string to a pchar?

>i allready know how to convert a pchartostr:

>Function PCharToStr( p: pChar; Len: WORD ): String;
>Var
>   s: String;
>Begin
>  SetLength( s, Len );
>  Move( p^, s[1], Len );
>  If POS( #0, s ) > 0 Then
>     SetLength( s, POS( #0, s ) - 1 );
>  RESULT := s;
>End

>please help,

>Vincent

Re:convert string to pchar


Quote
Vincent Vijt wrote:

> hi,

> how do i convert a string to a pchar?

it's easy;  here are 2 ways:

PChar('theString')
or
'theString' + #0

Re:convert string to pchar


Im Artikel <7ufrer$er...@news.planetinternet.be>, "Vincent Vijt"
<vi...@planetinternet.be> schreibt:

Quote
>how do i convert a string to a pchar?

In D4 you can use PChar(some_string). I don't know what will happen then, but
since dynamic strings are zero-terminated, you'll get a perfect C style string.
You can add the zero terminator to ShortString as well, provided at least 1
additional char is available in the string:

  str[Length[str]+1] := #0;
  charptr := @str[1];

DoDi

Other Threads