zstring vs string length

New to FreeBASIC? Post your questions here.
Post Reply
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

zstring vs string length

Post by StillLearning »

Using this example:

Code: Select all

Dim Parts as zstring*40
Dim Bins(1  to 3) as string
Parts = "staples"
Bins(1) = Parts
Bins(2) = ""staples"
'Notice the string length for Bins(1) <> Bins(2)

'Now any time Bins() is printed it will always have a additional byte. Is there a easy way to always have a dynamic string be the same size without
having to change its size by using poke?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: zstring vs string length

Post by fxm »

I do not understand your question.
Bins(1) and Bins(2) have the same size (7 characters).
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: zstring vs string length

Post by dodicat »

StillLearning wrote: Oct 11, 2022 6:24 Using this example:

Code: Select all

Dim Parts as zstring*40
Dim Bins(1  to 3) as string
Parts = "staples"
Bins(1) = Parts
Bins(2) = ""staples"
'Notice the string length for Bins(1) <> Bins(2)

'Now any time Bins() is printed it will always have a additional byte. Is there a easy way to always have a dynamic string be the same size without
having to change its size by using poke?
are you sure you are not using
print len(bin(1))
print len(bin(2))
(Because I have inadvertently did just that)
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

Re: zstring vs string length

Post by StillLearning »

I am sorry I did not include some code that was needed. here is the corrected listing:

Code: Select all

Dim Parts as zstring*40
Dim a as zstring*2
Dim Bins(1 to 3) as string
Parts = ""
Print "enter staples"
do
  a=""
  while a=""
    a=inkey
  wend
  print a;
  Parts = Parts + a
loop while a <> chr(13)
Parts[Len(Parts)] = 0
Bins(1) = Parts
Bins(2) = "staples"

print "length Bins(1) = ";len(Bins(1))
print "length Bins(2) = ";len(Bins(2))
Notice the string length for Bins(1) <> Bins(2).'
Why is this? What am I not seeing?

Now any time Bins() is printed it will always have a additional byte. Is there
a easy way to always have a dynamic string be the same size without having to change its size by using poke?
fxm
Moderator
Posts: 12081
Joined: Apr 22, 2009 12:46
Location: Paris suburbs, FRANCE

Re: zstring vs string length

Post by fxm »

The terminal character ('chr(13)') of the character input loop should not be appended to the 'Parts' string:

Code: Select all

Dim Parts as zstring*40
Dim a as zstring*2
Dim Bins(1 to 3) as string
Parts = ""
Print "enter staples"
do
  a=""
  while a=""
    a=inkey
    sleep 10  '' so as not to hog the CPU
  wend
  if a <> chr(13) then
    print a;
    Parts = Parts + a
  else
    print
  end if
loop while a <> chr(13)
'Parts[Len(Parts)] = 0  '' useless
Bins(1) = Parts
Bins(2) = "staples"

print "length Bins(1) = ";len(Bins(1))
print "length Bins(2) = ";len(Bins(2))

StillLearning wrote: Oct 12, 2022 5:29 Notice the string length for Bins(1) <> Bins(2).'
Why is this? What am I not seeing?
- 'Bins(1)' ('=Parts') is the concatenation of the characters entered in the loop.
- 'Bins(2)' = "staples"
Last edited by fxm on Oct 12, 2022 11:57, edited 1 time in total.
Reason: Correction of a typo ('Bin' instead of 'Bins') thanks to the dodicat remark.
dodicat
Posts: 7976
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: zstring vs string length

Post by dodicat »

instead of using
Parts[Len(Parts)] = 0
use
Parts[Len(Parts)-1] = 0
this correctly makes the last character chr(0) which will take the place of chr(13) and abruptly stop the zstring at the correct length.
fxm
- 'Bin(1)' ('=Parts') is the concatenation of the characters entered in the loop.
- 'bin(2)' = "staples"
The same Bin I wrongly used in my test.
StillLearning
Posts: 54
Joined: Aug 27, 2019 22:22

Re: zstring vs string length

Post by StillLearning »

Thanks everybody . I see the error. Lesson learned.
Post Reply