Forum - MCS Electronics

 

FAQFAQ SearchSearch RegisterRegister Log inLog in

Problem with 'Mid' and/or 'Left' functions

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

Bascom Member



Joined: 19 Dec 2015
Posts: 7
Location: Flanders

belgium.gif
PostPosted: Fri Apr 11, 2025 8:55 pm    Post subject: Problem with 'Mid' and/or 'Left' functions Reply with quote

They don't work like before. I am updating a 5 years old Bascom-Avr program. Is does not work anymore.
I isolated the problem in a testprogram.
The 3 Print-statements should give the same output.
Not anymore now.

Quote:

Dim Buf(50) As Byte
Dim Bufc As String * 50 At Buf Overlay

Bufc = "$P"
Mid(Bufc,3,2) = "1;"
Mid(Bufc,5,2) = "$T"
Print left(Bufc,6)

Mid(Bufc,1,2) = "$Q"
Mid(Bufc,3,2) = "2;"
Mid(Bufc,5,2) = "$T"
Print left(Bufc,6)

Mid(Bufc,1,6) = "$Q3;$T"
Print left(Bufc,6)

Bufc = "$R4;$T"
Print left(Bufc,6)


In the previous version of my program (5 years ago) I used the first compilation of the string. It worked fine, the AVR printed all the 6 character. Now, it prints only 2 chars.
What can be wrong ?



(BASCOM-AVR version : 2.0.8.6 )
Back to top
View user's profile
albertsm

Administrator



Joined: 09 Apr 2004
Posts: 6173
Location: Holland

blank.gif
PostPosted: Sat Apr 12, 2025 8:10 am    Post subject: Reply with quote

the output is correct. in older versions where you get different output there was a bug which was fixed.
MID statement may not write beyond the length of the string and that is what you do.
the reason is that it is potential unsafe. VB will not allow it either.
To fix your code simple use + SomeString = Somestring + "QQ"
Or make the string filled with spaces.

_________________
Mark
Back to top
View user's profile Visit poster's website
MarkDM2

Bascom Member



Joined: 19 Dec 2015
Posts: 7
Location: Flanders

belgium.gif
PostPosted: Sat Apr 12, 2025 8:56 pm    Post subject: Reply with quote

Thank you for the reply.

I modified the first line of the testprogram:

Quote:
Bufc = "$P3456"

Then I overwrite with 'mid' function.

It works fine.
Back to top
View user's profile
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