Board index » delphi » Strings and Null terminated strings

Strings and Null terminated strings

Hi,

I calling a Windows API function that gives me back a PChar.

I convert this to a normal string using: SetString(mystring, mypchar,
sizeofpchar).

After this mystring still contains a #0 after the normal characters.

So after SetString my string look like this in Debug mode: 'my_text'#0

Is this normal behaviour? Can I get rid of it?

Why would I get rid of it?

When doing a string compare, using the = operator, strings don't math
with eachother because of the last #0.

Solution to this, is to remove the last 2 characters of the string,
but I not really like this option.

Or is there another stringcompare function that works with strings
ending on #0?

Thanks!!!

Joris

 

Re:Strings and Null terminated strings


Quote
"Joris Kempen" <joris.kem...@groupsupport.com> wrote in message
> I calling a Windows API function that gives me back a PChar.

> I convert this to a normal string using: SetString(mystring, mypchar,
> sizeofpchar).

Are you using Length (myPChar) as the length parameter?

Quote

> After this mystring still contains a #0 after the normal characters.

All long strings have a null at Length (text) + 1. This is to make a pChar
typecast work. However, it appears from your post that you also have a null
at Length (text), which is probably caused by an incorrect length being
passed to SetString.

Re:Strings and Null terminated strings


On 20 Mar 2002 06:43:39 -0800, joris.kem...@groupsupport.com (Joris

Quote
Kempen) wrote:
>I calling a Windows API function that gives me back a PChar.

>I convert this to a normal string using: SetString(mystring, mypchar,
>sizeofpchar).

Wouldn't using StrLen (pchar) be better?

--
Bob Cousins

Re:Strings and Null terminated strings


In article <ac799ae6.0203200643.18b4a...@posting.google.com>,

Quote
joris.kem...@groupsupport.com (Joris Kempen) writes:
>I convert this to a normal string using: SetString(mystring, mypchar,
>sizeofpchar).

Some API's include the final #0 in the length count, and most don't. Look at
the API description to be sure.

Trim() will also get rid of the trailing #0.

Alan Lloyd
alangll...@aol.com

Re:Strings and Null terminated strings


Quote
Joris Kempen <joris.kem...@groupsupport.com> wrote in message

news:ac799ae6.0203200643.18b4aaf8@posting.google.com...

Quote
> Hi,

> I calling a Windows API function that gives me back a PChar.

> I convert this to a normal string using: SetString(mystring, mypchar,
> sizeofpchar).

> After this mystring still contains a #0 after the normal characters.

> So after SetString my string look like this in Debug mode: 'my_text'#0

> Is this normal behaviour? Can I get rid of it?

Yes, But do you really mean this?

SetString(mystring, mypchar, sizeofpchar)

or this?

SetString(mystring, mypchar, length(mypchar))

Dave

Re:Strings and Null terminated strings


Quote
> I calling a Windows API function that gives me back a PChar.

> I convert this to a normal string using: SetString(mystring, mypchar,
> sizeofpchar).

> After this mystring still contains a #0 after the normal characters.

> So after SetString my string look like this in Debug mode: 'my_text'#0

> Is this normal behaviour? Can I get rid of it?

> Why would I get rid of it?

> When doing a string compare, using the = operator, strings don't math
> with eachother because of the last #0.

> Solution to this, is to remove the last 2 characters of the string,
> but I not really like this option.

> Or is there another stringcompare function that works with strings
> ending on #0?

I've seen answers to your questions.... I just want to ask you why don't you
use simple type casting, I mean "MyString := string(PCharString)".

Best regards.
 Evgeny.

Re:Strings and Null terminated strings


"Evgeny V. Levashov" <eug...@mebelmassiv.tula.ru> wrote in message <news:160647889@mebelmassiv.tula.ru>...

Quote
> I've seen answers to your questions.... I just want to ask you why don't you
> use simple type casting, I mean "MyString := string(PCharString)".

Thanks for all your suggestions people.

Now I going to figure out how these strings really work.

Now I have a working solution by cutting of one char after the SetString function.

But probably I using the SetString in the wrong way...

I'm going to figure it out now.

Thanks for your help.

Re:Strings and Null terminated strings


Evgeny V. Levashov <eug...@mebelmassiv.tula.ru> wrote in message
news:160647889@mebelmassiv.tula.ru...

[snip]

Quote
> I've seen answers to your questions.... I just want to ask you why don't
you
> use simple type casting, I mean "MyString := string(PCharString)".

Under normal circumstances the assignement, MyString := PCharString,  will
work just fine with no need for an explicit cast.  However, a dll function
of the type

MyDLLProcedure(szText: PChar)

which expects to write a null terminated string back to the location pointed
to by szText needs more care. If we declare a Delphi string to hold the text
that is written back then we need to explicitly adjust the length of the
string to its null terminated length since the compiler can't guess what is
happening here.
var
    sText: string;
begin
    SetLength(sText, 50);
    MyDLLProcedure(PChar(sText));
    SetLength(sText, Length(PChar(sText)));

However if we declare a character array to hold the string, then a simple
assignment to a string type will work.
var
    Buff: array[0..49] of char;
    sText: string;
begin
    MyDLLProcedure(Buff);
    sText := Buff;

I guess the first case is messier because we are not sticking to Delphi's
strong typing as we are in the second case.

Dave

Other Threads