Page 1 of 1

CHR(0) AND #NULL$ problem

Posted: Sun Feb 28, 2016 8:41 pm
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)
'

Re: CHR(0) AND #NULL$ problem

Posted: Sun Feb 28, 2016 9:01 pm
by User_Russian
c.s=Chr(0) similarly c.s=""

Re: CHR(0) AND #NULL$ problem

Posted: Sun Feb 28, 2016 10:26 pm
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.

Re: CHR(0) AND #NULL$ problem

Posted: Mon Feb 29, 2016 11:08 am
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".

Re: CHR(0) AND #NULL$ problem

Posted: Mon Feb 29, 2016 11:34 am
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))

Re: CHR(0) AND #NULL$ problem

Posted: Mon Feb 29, 2016 11:41 am
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:

Re: CHR(0) AND #NULL$ problem

Posted: Wed Mar 09, 2016 9:50 pm
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.

Re: CHR(0) AND #NULL$ problem

Posted: Thu Mar 10, 2016 9:20 am
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]

Re: CHR(0) AND #NULL$ problem

Posted: Thu Mar 10, 2016 7:42 pm
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.