How to add null characters?

New to FreeBASIC? Post your questions here.
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

How to add null characters?

Postby newbieforever » Jan 23, 2019 19:53

Another newbie question:

I have a string of bytes (data read from a binary file by LoadFile()) in which I have to replace a substring by another string. This other string, rpl, is shorter than the substring, and should be extended to the length of the substring by adding a number of null characters chr(0). E.g.:

Code: Select all

'Dim As String rpl
'rpl = ...
rpl = rpl + chr(0) + chr(0) + chr(0) + chr(0)

But this does not work, rpl is not extended by this way. What can I do?

So long!
Last edited by newbieforever on Jan 23, 2019 22:36, edited 3 times in total.
badidea
Posts: 1608
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to add zero characters?

Postby badidea » Jan 23, 2019 20:02

Why is that not working?

Code: Select all

Dim As String rpl = "test"
print  "|" + rpl + "|"; len(rpl)
rpl = rpl + chr(0) + chr(0) + chr(0) + chr(0)
print  "|" + rpl + "|"; len(rpl)
for i as integer = 0 to len(rpl)-1
   print rpl[i]; " ";
next

Output here:

Code: Select all

|test| 4
|test    | 8
116 101 115 116 0 0 0 0
dodicat
Posts: 6013
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to add zero characters?

Postby dodicat » Jan 23, 2019 21:19

Make up a little function for the task.

Code: Select all

 


Function SAR(s0 As String,search As String,replace As String) As String
    Dim s As String=s0
    var position=Instr(s,search)
    While position>0
        s=Mid(s,1,position-1) & replace & Mid(s,position+Len(search))
        position=Instr(position+Len(replace),s,search)
    Wend
    SAR=s
End Function

function Pad(byval b as string,byval padme as string) as string
padme+=string(len(b)-len(padme),0)
return padme
end function

dim as string g="abcdefghifjklmno"
dim as string h="abc"

 print "'";g;"'"
 print "'";pad(g,h);"'"
 print
 print
 dim as string a
 for n as long=1 to 500
     a+=chr(48 + rnd*9)
 next
 print a
 

a= SAR(a,"34458225334284367436945665877883915409053775627478143871602351430", _
     pad("34458225334284367436945665877883915409053775627478143871602351430","HELLO"))
 print
 print a
 sleep
 
   
     
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: How to add zero characters?

Postby newbieforever » Jan 23, 2019 22:36

Sorry...sorry...sorry... I reduced my code to a too simplified example. Your answer, badidea, showed me that I located my error in the wrong place. Of course, this is working fine, and I discovered now my error.

Thank you both, badidea & dodicat, for your helpful hints!!!
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: How to add null characters?

Postby newbieforever » Jan 23, 2019 22:48

Btw: Can this be shortened (for a larger number of null characters)?
rpl = rpl + chr(0) + chr(0) + ...
badidea
Posts: 1608
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to add null characters?

Postby badidea » Jan 23, 2019 22:48

No problem, be aware that when you convert to zstring, things are different:

Code: Select all

Dim As String rpl = "test"
print  "|" + rpl + "|"; len(rpl)
rpl = rpl + chr(0) + chr(0) + chr(0) + chr(0)
print  "|" + rpl + "|"; len(rpl)
for i as integer = 0 to len(rpl)-1
   print rpl[i]; " ";
next
print
dim as zstring ptr pStr = strptr(rpl)
print "|" + *pStr + "|"; len(*pStr)
badidea
Posts: 1608
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to add null characters?

Postby badidea » Jan 23, 2019 22:49

newbieforever wrote:Btw: Can this be shortened (for a larger number of null characters)?
rpl = rpl + chr(0) + chr(0) + ...

rpl = rpl + string(4, chr(0)) ?
or
rpl += string(4, chr(0))
newbieforever
Posts: 117
Joined: Jun 21, 2018 11:14

Re: How to add null characters?

Postby newbieforever » Jan 23, 2019 23:01

Yessss, that's it!!!

So long!
dodicat
Posts: 6013
Joined: Jan 10, 2006 20:30
Location: Scotland

Re: How to add null characters?

Postby dodicat » Jan 23, 2019 23:11

The string function is also overloaded.

Code: Select all

print string(10,48)+string(10,0) + string(10,49)
sleep
badidea
Posts: 1608
Joined: May 24, 2007 22:10
Location: The Netherlands

Re: How to add null characters?

Postby badidea » Jan 23, 2019 23:20

So, even shorter:
rpl += string(4, 0)

Return to “Beginners”

Who is online

Users browsing this forum: No registered users and 5 guests