Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

adding a null to a string

 
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR
View previous topic :: View next topic  
Author Message
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 110
Location: The Netherlands

netherlands.gif
PostPosted: Mon Mar 02, 2020 4:53 pm    Post subject: adding a null to a string Reply with quote

Hello,

Is there as possibility the add manually a "null" to a string

for example

Code:

$regfile = "m88pdef.dat"
$crystal = 4000000
$hwstack=40
$swstack=16
$framesize = 32

Dim Mystring As String * 40
Dim Aa As Byte

For Aa = 1 To 10                                            'fill the string with 10 chars
   Mid(mystring , Aa , 1) = "a"

Next Teller

Print Len(mystring)
'result = 10

For Aa = 1 To 4                                             'fill the string with 4 chars
   Mid(mystring , Aa , 1) = "b"

Next Teller

Print Len(mystring)
'result = 10
'after the fourth  "b" there must be a "null"to terminate the string

End
 


regards Tiny

(BASCOM-AVR version : 2.0.8.2 )
Back to top
View user's profile Visit poster's website
MWS

Bascom Member



Joined: 22 Aug 2009
Posts: 2335

blank.gif
PostPosted: Mon Mar 02, 2020 7:05 pm    Post subject: Reply with quote

Your code won't compile.

Beside this, overlay mystring with a byte-array and assign the 0 there.
Back to top
View user's profile
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 110
Location: The Netherlands

netherlands.gif
PostPosted: Mon Mar 02, 2020 10:27 pm    Post subject: Reply with quote

Sorry for the wrong sample code

Your solution for using an array as overlay works, Thanks

Code:

$regfile = "m88pdef.dat"
$crystal = 4000000
$hwstack=40
$swstack = 16
$framesize = 32

Dim Mystring As String * 40
Dim Bytes(40) As Byte At Mystring Overlay
Dim Aa As Byte

For Aa = 1 To 10                                            'fill the string with 10 chars
   Mid(mystring , Aa , 1) = "a"

Next Aa

Print Len(mystring)
'result = 10

For Aa = 1 To 4                                             'fill the string with 4 chars
   Mid(mystring , Aa , 1) = "b"

Next Aa
Bytes(5) = 0

Print Len(mystring)
'result = 10
'after the fourth  "b" there must be a "null"to terminate the string

End
 
Back to top
View user's profile Visit poster's website
AdrianJ

Bascom Expert



Joined: 16 Jan 2006
Posts: 2483
Location: Queensland

australia.gif
PostPosted: Tue Mar 03, 2020 2:28 am    Post subject: Reply with quote

Quote:

For Aa = 1 To 4 'fill the string with 4 chars
Mid(mystring , Aa , 1) = "b"

Next Aa
Bytes(5) = 0


You can also do it without the overlay:

Code:

For Aa = 1 To 4                                             'fill the string with 4 chars
   Mid(mystring , Aa , 1) = "b"

Next Aa
' then:
Mid(mystring,5,1) = 0

'note this is NOT the same as:
Mid(mystring,5,1) = "0"

 

Certainly the overlay works, and for some things, its a very elegant solution, but it is a bit obscure.

_________________
Adrian Jansen
Computer language is a framework for creativity
Back to top
View user's profile Visit poster's website
EDC

Bascom Expert



Joined: 26 Mar 2014
Posts: 1135

poland.gif
PostPosted: Tue Mar 03, 2020 9:20 am    Post subject: Reply with quote

For "same characters strings" you can use String function instead, nonefficient here, For-Next loop.
Code:
Dim Mystring As String * 40

Mystring = String(10 , 97)                                  '10x "a"
Print Mystring
Mystring = String(4 , 98)                                   '4x "b"
Print Mystring

End
Back to top
View user's profile Visit poster's website
Tiny

Bascom Member



Joined: 10 Nov 2010
Posts: 110
Location: The Netherlands

netherlands.gif
PostPosted: Wed Mar 04, 2020 7:46 am    Post subject: Reply with quote

Hey,

thanks everyone for the solutions provided.

Regards Tiny
Back to top
View user's profile Visit poster's website
Display posts from previous:   
Post new topic   Reply to topic    www.mcselec.com Forum Index -> BASCOM-AVR All times are GMT + 1 Hour
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
You cannot attach files in this forum
You cannot download files in this forum