CHR(0) AND #NULL$ problem

Just starting out? Need help? Post your questions and find answers here.
zgneng
User
User
Posts: 29
Joined: Sun Jun 28, 2015 9:04 am

CHR(0) AND #NULL$ problem

Post by zgneng »

Code: Select all

a.s="a"

b.s="b"

c.s=Chr(0)

d.s=#Null$


MessageRequester("",a+c+b,0)

MessageRequester("",a+d+b,0)

The correct value should be output only 'a

PUB is not truncated Chr (0)
'
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CHR(0) AND #NULL$ problem

Post by User_Russian »

c.s=Chr(0) similarly c.s=""
User avatar
Demivec
Addict
Addict
Posts: 4263
Joined: Mon Jul 25, 2005 3:51 pm
Location: Utah, USA

Re: CHR(0) AND #NULL$ problem

Post by Demivec »

zgneng wrote:The correct value should be output only 'a

PUB is not truncated Chr (0)
'
A #Null$ or Chr(0) is not part of a string, it is the end of a string.

If this wasn't so then when you displayed 'a.s + d.s' you would only see 'a.s' because it ends with a Chr(0).
The strings are concatenated by adding together all of their contents before each Null and then a Null is placed at the end.
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CHR(0) AND #NULL$ problem

Post by Dude »

User_Russian wrote:c.s=Chr(0) similarly c.s=""
Not so. If that were true, then anything following c.s would still be shown:

Code: Select all

a.s="a"
b.s="b"
c.s=""
d.s="d"

Debug a+b+c+d ; abd
Chr(0) means "end of string", not "empty string".
User_Russian
Addict
Addict
Posts: 1520
Joined: Wed Nov 12, 2008 5:01 pm
Location: Russia

Re: CHR(0) AND #NULL$ problem

Post by User_Russian »

Dude wrote:Chr(0) means "end of string", not "empty string".
Find a difference.

Code: Select all

c.s=""
ShowMemoryViewer(@c, SizeOf(Character))

Code: Select all

c.s=Chr(0)
ShowMemoryViewer(@c, SizeOf(Character))
Dude
Addict
Addict
Posts: 1907
Joined: Mon Feb 16, 2015 2:49 pm

Re: CHR(0) AND #NULL$ problem

Post by Dude »

Holy crap... you're right! :shock: Since when is this possible:

Code: Select all

string$="a"+"b"+Chr(0)+"d"
Debug string$ ; abd
I'm 99% sure this NEVER used to work in older PureBasics! What the hell?

[Edit] Found it. With v4.61 of PureBasic, Chr(0) did act as end of string:

Code: Select all

string$="a"+"b"+Chr(0)+"d"
Debug string$ ; ab with PureBasic v4.61
Never knew it changed! :shock:
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: CHR(0) AND #NULL$ problem

Post by RichAlgeni »

You can use Chr(0) inside a string accessed with a pointer. Chr(0)'s are often sent and received in network packets.
Joris
Addict
Addict
Posts: 890
Joined: Fri Oct 16, 2009 10:12 am
Location: BE

Re: CHR(0) AND #NULL$ problem

Post by Joris »

All still confusing, also like in the one here :
http://www.purebasic.fr/english/viewtop ... ctureunion

Code: Select all

Structure TEST
  StructureUnion
    st.s{#MAXPNAMELEN * 2}   
    io.s{#MAXPNAMELEN}[2]   
  EndStructureUnion
EndStructure

Global d.TEST, s.s{#MAXPNAMELEN}

;s="a string shorter then 32"                         ;s doesn't help while s =>  s.s{#MAXPNAMELEN}
;d\io[0]=s
;d\io[0]="a string exact length of 32 byte"
d\io[0]="a string shorter then 32"
d\io[1]="added to a string longer then 32.."
Debug d\io[0]
Debug d\io[1]
Debug d\st                                                 ;both io are only kept if io[0] is at least 32 bytes long

d\st="a string shorter then 32 added to a string longer then 32.."
Debug d\io[0]
Debug d\io[1]
Yeah I know, but keep in mind ... Leonardo da Vinci was also an autodidact.
User avatar
RichAlgeni
Addict
Addict
Posts: 935
Joined: Wed Sep 22, 2010 1:50 am
Location: Bradenton, FL

Re: CHR(0) AND #NULL$ problem

Post by RichAlgeni »

Do you have to you fixed length strings???

Code: Select all

EnableExplicit

OpenConsole()

Structure TEST
  StructureUnion
    st.s[2]
    io.s[2]
  EndStructureUnion
EndStructure

Global d.TEST, s.s

d\io[0]="a string shorter then 32|"
d\io[1]="added to a string longer then 32.."

PrintN("")

PrintN("d\io[0] = " + d\io[0])
PrintN("d\io[1] = " + d\io[1])

PrintN("")

d\st=d\io[0] + d\io[1]

PrintN("d\st = " + d\st)                              ;both io are only kept if io[0] is at least 32 bytes long

Input()

CloseConsole()

End
This seems to work fine.
Post Reply